Chapter 3 binary codes
Chapter 3:
Binary Codes
Contents
1. Objectif of this part......................................................................................................................... 2
2. Binary Coded Decimal (BCD) .......................................................................................................... 2
2.1. The BCD Addition ................................................................................................................... 3
2.2. The BCD Substruction: ........................................................................................................... 4
3. The Gray Code Or the reflected binary code ................................................................................. 4
3.1. Generation of Gray codes with "n" bits ................................................................................. 5
3.2. Binary–Gray Code Conversion ................................................................................................ 6
3.3. Gray-to-Binary Code Conversion ............................................................................................ 6
4. Alphanumeric codes ....................................................................................................................... 7
4.1. ASCII code................................................................................................................................ 7
4.2. EBCDIC code ............................................................................................................................ 8
1.3. UNICODE code......................................................................................................................... 8
Chapter 3 binary codes
1. Objectif of this part
This chapter is a continuation of what we learned in Chapter 1, where we studied number
systems. We explored the decimal system, which we use every day, and introduced binary,
hexadecimal, and octal systems. Binary is the most important for computers, while
hexadecimal and octal make working with binary numbers easier. In this chapter, we will go
further and learn about special binary codes used to represent data and handle tasks like error
detection and correction.
2. Binary Coded Decimal (BCD)
The Binary Coded Decimal (BCD) is a binary code used to represent decimal numbers in binary
form. Each decimal digit is replaced by its four-bit binary equivalent for both integer and
fractional parts. For example, the BCD equivalent of (23.15)10 is (0010 0011.0001 0101)BCD.
The most common BCD code is the 8421 BCD,
where 23, 22, 21, 20 are the weights of the bits
in each four-bit group, starting from the most
significant bit (MSB) to the least significant bit
(LSB). This makes it a weighted code. Other
weighted BCD codes include 4221 and 5421
BCD, where the numbers represent the
weights of the bits.
The main advantage of the 8421 BCD is the ease of conversion between decimal numbers and
their binary equivalents, as shown in the table of binary combinations for decimal digits (0–9).
23(8) 22(4) 21(2) 20 (1) Decimal
0 0 0 0 0
0 0 0 1 1
0 0 1 0 2
0 0 1 1 3
Valid bcd code 0 1 0 0 4
0 1 0 1 5
0 1 1 0 6
0 1 1 1 7
1 0 0 0 8
1 0 0 1 9
1 0 1 0 10
1 0 1 1 11
1 1 0 0 12
1 1 0 1 13
1 1 1 0 14 InValid bcd code
1 1 1 1 15
In BCD code, four bits are used to represent numbers, allowing for 16 possible combinations (0000 to
1111). However, only 10 of these combinations (0000 to 1001) are valid because they correspond to
Chapter 3 binary codes
the decimal digits 0 through 9. The remaining six combinations—1010, 1011, 1100, 1101, 1110, and
1111—are invalid in BCD and cannot be used.
Example Convert each of the following decimal numbers to BCD:
a. 35 b. 98 c. 170 d. 2469
decimal 35 98 170 2469
BCD 0011 0101 1001 1000 0001 0111 0000 0010 0100 0110 1001
It is equally easy to determine a decimal number from a BCD number. Start at the right-most bit
and break the code into groups of four bits. Then write the decimal digit represented by each 4-
bit group.
Example Convert each of the following BCD codes to decimal:
a. 10000110 b. 001101010001 c. 100101000111 0000
BCD 1000 0110 0011 0101 0001 1001 0100 0111 0000
decimal 86 351 9470
Applications BCD code is commonly used in devices like digital clocks, thermometers, and meters
with seven-segment displays because it simplifies the display of decimal numbers. While BCD is
less efficient than straight binary for complex calculations, it is well-suited for applications with
minimal processing needs, such as digital thermometers.
2.1. The BCD Addition
BCD is a numerical code and can be used in arithmetic operations. Here is how to add two BCD
numbers:
• Step 1: Add the two BCD numbers, using the rules for binary addition.
• Step 2: If a 4-bit sum is ≤9, it is a valid BCD result.
• Step 3: If a 4-bit sum is > 9, or if a carry out of the 4-bit group is generated, it is an invalid
result.
• Correction: Add 6 (0110) to the 4-bit invalid sum. This skips the six invalid states and
restores the value to the BCD format.
• If a carry results when 6 is added, simply add the carry to the next 4-bit group
Example 1: Add the following BCD numbers:
a. 00100011 + 00010101 b. 10000110 + 00010011 c. 010001010000 + 010000010111
0010 0011 23 1000 0110 86 0100 0101 0000
+ 0001 0101 + 15 + 0001 0011 + 13 + 0100 0001 0111
0011 1000 38 1001 1001 99 1000 0110 0111
Valid BCD number (<=9)
Chapter 3 binary codes
Example 2: Add the following BCD numbers:
a. 1001 + 0100 b. 1001 + 1001 c. 00010110 + 00010101 d. 01100111 + 01010011
The operation needs a correction so in this case we add 6 (0110)2
1001 1000 0001 0101 0101 0011
+ 0101 + 1001 0001 0110 + 0110 0111
1
1110 0000 0001 0010 11011 1
1011 1010
+ 0110 + 1 0110 + 1 0110 + 1 0110 0110
0001 0100 0001 0111 0011 0001 0001 0010 0000
Invalid BCD Invalid code because Right group is invalid Both groups is invalid
number (>9) of carry. Add 6 to get (>9), left group is valid. (>9). Add 6 to invalid
Add 6 to get Valid Add 6 to invalid code. codes. Add carry, to
Valid BCD Add carry, to next next group. To get a
number group. To get a Valid Valid BCD number
BCD number
2.2. The BCD Substruction:
Similarly, BCD subtraction is performed in two steps:
• Step 1: Calculate the subtraction of each pair of four-bit groups individually, borrowing a '1'
from the next group if necessary.
• Step 2: Perform corrections from right to left by subtracting (0110) from each group of four
bits exceeding 1001 (the value 9) or from each group that has borrowed a '1' from the next
group.
1001 0011 0110 0010 0110 0101
- 0101 - 0001 1000 - 0001 1000 0111
0100 0001 1110 0000 1101 1110
- 0110 - 0110 0110
0011 0001 0000 0111 1000
Valid BCD Right group is invalid Both groups is invalid
number (>9), left group is valid. due to (>9)and the
(<=9) Sub 6 to invalid code, borrow. Sub 6 to
to get a Valid BCD invalid codes to get a
number Valid BCD number
3. The Gray Code Or the reflected binary code
The Gray code is unweighted and is not an arithmetic code; that is, there are no specific weights
assigned to the bit positions.
The important note of the Gray code is that only a single bit change from one binary code to the next
in sequence.
Chapter 3 binary codes
Table below listing the 4-bit Gray code for decimal numbers 0 through 15. Binary numbers are shown
in the table for reference. Like binary numbers, the Gray code can have any number of bits. Notice the
single-bit change between successive Gray code words.
Decimal Binary Gray code Decimal Binary Gray code
0 0000 0000 8 1000 1100
1 0001 0001 9 1001 1101
2 0010 0011 10 1010 1111
3 0011 0010 11 1011 1110
4 0100 0110 12 1100 1010
5 0101 0111 13 1101 1011
6 0110 0101 14 1110 1001
7 0111 0100 15 1111 1000
3.1. Generation of Gray codes with "n" bits
0000
0001
000 0011
001
0 00 0010
1 011 0110
01
Mirror Mirror 11 010
1 Mirror 0111
0 110 0101
10
111 0100
101 Mirror
1 bit 2 bits 1100
100 1101
1111
3 bits
1110
1010
1011
1001
1000
4 bits
We can construct an n-bit Gray code from an (n-1)-bit Gray code as follows: Starting by forming the
two numbers with a single bit (the 0 and the 1), then we copy the words from the original code, with
a '0' added before each word, followed by the words from the same code, taken in reverse order, with
a '1' added before each word.
Chapter 3 binary codes
3.2. Binary–Gray Code Conversion
A given binary number can be converted into its Gray code equivalent by going through the following
steps:
• The most significant bit (left-most) in the Gray code is the same as the corresponding MSB in
the binary number.
• Going from left to right, add each adjacent pair of binary code bits to get the next Gray code
bit. When summing 1+1, we consider only the result (0), and we ignore the carry bit (1).
Example 1: the conversion of the binary number 10110 to Gray code is as follows:
1 + 0 + 1 + 1 + 0
1 1 1 0 1
The Gray code is 11101.
Example 2: Convert the binary number 11001 to Gray code.
1 + 1 + 0 + 0 + 1
1 0 1 0 1
The Gray code is 10101.
3.3. Gray-to-Binary Code Conversion
To convert from Gray code to binary, use a similar method; however, there are some differences. The
following rules apply:
1. The most significant bit (left-most) in the binary code is the same as the corresponding bit in the
Gray code.
2. Add each binary code bit generated to the Gray code bit in the next adjacent position. Discard
carries.
Example 1: the conversion of the Gray code word 11011 to binary is as follows:
1 1 0 1 1
+ + +
+
1 0 0 0 1
The binary number is 10010.
Chapter 3 binary codes
Nb:
- Gray code is used for labelling the axes of Karnaugh maps: a graphical technique used for
minimization of Boolean expressions (next Chapter)
- Using Gray-code in address program memory in computers minimizes power consumption
(one bit changing)
4. Alphanumeric codes
Alphanumeric codes, also called character codes, are binary codes used to represent letters of the
alphabet, numbers, mathematical symbols and punctuation marks, in a form that is understandable
and processable by a computer. These codes enable us to interface input–output devices such as
keyboards, printers, VDUs, etc., with the computer.
4.1. ASCII code
- ASCII, which stands for American Standard Code for Information Interchange, originally
developed in the 1960s, ASCII has been a fundamental encoding standard for text-based
communication and data exchange in computing.
- The ascii code is a character encoding standard that represents text and control characters in
computers, communication equipment, and other devices that use text.
- ASCII uses a 7-bit binary code to represent 128 characters and symbols.
Characters (First 32 ASCII Characters):
These characters are not shown on the screen; they're used to control devices and processes.
- Examples include "null," "line feed," "start of text," and "escape."
Graphic Symbols (Remaining ASCII Characters):
These characters can be seen and printed. They include:
✓ lowercase (like a, b, c, etc.) (presented from decimal 97 to 122)
✓ uppercase (like A, B, C, etc.) letters (presented from decimal 65 to 90),
✓ numbers (0 to 9), punctuation marks (like commas and periods), and common symbols.
In the ASCII standard, each character is assigned a numeric value ranging from 0 to 127, which can be
represented in binary, decimal, or hexadecimal form. The following table presents a listing of the ASCII
code showing its three representations for each character and symbol.
The left section of the table lists the names of the 32 control characters (00 through 1F hexadecimal).
The graphic symbols are listed in the rest of the table (20 through 7F hexadecimal).
Chapter 3 binary codes
4.2. EBCDIC code
Pronounced ‘eb-si-dik’ : Extended Binary Coded Decimal Interchange Code
It is an eight-bit code and can accommodate up to
256 characters (alphanumeric characters,
punctuation and other symbols
Used mainly in IBM mainframe computers
The codes in the following table are in hexadecimal
Example1: what is the EBCDIC code of ‘N’, ‘@’ and ‘z’
From the table:
‘N’ = (D5)16 = (1101 0101)2
‘@’ = (7C)16 = (0111 1100)2
‘z’ = (A9)16 = (1010 1001)2
Example2: what is the text equivalent to the following EBCDIC code
From the table: (C9 D5 86 96)16 = (11001001 11010101 10000110 10010110)2= INfo
1.3. UNICODE code
As briefly mentioned in the earlier sections, encodings such as ASCII, EBCDIC and their variants do
not have a sufficient number of characters that deals with scripts and languages. do not permit
multilingual computer processing.
Chapter 3 binary codes
• Unicode, developed jointly by the Unicode Consortium and the International Organization for
Standardization (ISO), is the most complete character encoding scheme that allows text of all
forms and languages to be encoded for use by computers.
• It uses 16 bits or more for each character, offering a comprehensive and standardized method
for handling text in different languages and scripts.
• using eight bit words (for UTF-8), 16 bit words (for UTF-16) and 32 bit words (for UTF-32),
Unicode represents each character in a unique way by a number.
Bytes Unicode Format
1 0xxx xxxx
2 110xxxxx 10xxxxxx
3 1110xxxx 10xxxxxx 10xxxxxx
• Unicode can represent a much larger set of characters from various languages and scripts
worldwide. Such as English, French, Spanish, and German. Russian, Bulgarian, and Serbian.
Arabic also Greek alphabet. It includes Greek letters and symbols.
• Unicode assigns the first 128 code points to the same characters as ASCII to ensure
compatibility, making ASCII a subset of Unicode.
This standard has been adopted by such industry leaders as HP, IBM, Microsoft, Apple, Oracle,
Unisys, Sun, Sybase, SAP and many more.