CODES
BCD, XS-3, Gray Code, Alphanumeric Codes
(ASCII, EBCDIC), Error detecting and correcting
codes (Parity Code, Hamming Code)
Amraja Shivkar
BCD(Binary Coded Decimal) Code
• In this code each digit is represented by a 4-bit binary number.
• The positional weights assigned to the binary digits in BCD code are 8-4-2-1 with 1
corresponding to LSB and 8 corresponding to MSB.
Positional 8 4 2 1
Weights 23 22 21 20
MSB LSB
• Other BCD codes like 7-4-2-1, 5-4-2-1 etc also exist.
Conversion from decimal to BCD
• The decimal digits 0 to 9 are converted into BCD, exactly in the same way as binary.
Digital 0 1 2 3 4 5 6 7 8 9
BCD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Invalid BCD codes:
• With 4 bits we can represent total sixteen numbers (0000 to 1111) but in BCD only
first ten codes are used (0000 to 1001)
• Therefore remaining six codes (1010 to 1111)are invalid in BCD
Conversion of bigger decimal numbers to BCD:
• Express each decimal digit with its equivalent 4-bit BCD code
• Eg.: Convert (964)10 to its equivalent BCD code.
Decimal Number → 9 6 4
Binary Equivalent → 1001 0110 0100
There fore (964)10 = (1001 0110 0100)BCD
• Hence smallest number in BCD is 0000 i.e., 0 and largest is 1001 i.e., 9 after
which 10 will be expressed by combinations i.e., 0001 0000 and is known as
packed BCD
Comparison with Binary:
• Less efficient than binary, since conversion of a decimal number into BCD
needs more bits than in binary
Eg., (22)10 = (10110)2 = (0010 0010)BCD So BCD uses more bits than binary for
the same decimal number.
• BCD arithmetic is more complicated than binary arithmetic.
• BCD – decimal conversion is simpler than Binary – decimal conversion.
Advantages of BCD codes:
• Its similar to decimal number system.
• We need to remember binary equivalents of decimal numbers 0 to 9 only.
• Conversions from decimal to BCD or BCD to decimal is very simple and no
calculation is needed.
Disadvantages of BCD codes:
• Less efficient than binary, since conversion of a decimal number into BCD
needs more bits than in binary
• BCD arithmetic is more complicated than binary arithmetic.
-------------------------------------------------------------------------------------------------------------
Convert following decimal numbers to BCD:
(a) 164 (b) 4297 (c) 8065
Convert following BCD codes to decimal equivalent:
(a) 1001 1000 (b) 0001 0100 0110 (c) 0111 0011 0101
Convert following binary numbers to BCD codes: (Hint: convert to decimal first)
(a) 1100 (b) 10001 (c) 1010101
Convert following BCD codes to binary equivalent: (Hint: convert to decimal first)
(a) 0010 1000 (b) 1001 0111 (c) 1000 0000
XS-3 (Excess-3)Code
• Non-weighted code.
• Derived from BCD code (8-4-2-1 code)words by adding (0011)2 or (3)10 to each
code word.
Write each digit in 4-bit binary code + (0011)
Decimal BCD XS-3
• Therefore Hence smallest number in XS-3 is 0011 i.e., 0 and largest is 1100 i.e., 9
Decimal BCD XS-3
• XS-3 is a reflective code since code
0 0000 0011 for 9 is complement of code for 0,
1 0001 0100 code for 8 is complement of code for
2 0010 0101 1, code for 7 is complement of code
for 2, code for 6 is complement of
3 0011 0110
code for 3, code for 5 is complement
4 0100 0111 of code for 4.
5 0101 1000 • It is a sequential code since each
6 0110 1001 number is 1 binary bit greater than
7 0111 1010 its preceding number.E
8 1000 1011
9 1001 1100
Conversion of decimal numbers XS-3 code:
• Eg.: Convert (964)10 to its equivalent XS-3 code.
Decimal Number → 9 6 4
XS-3 Equivalent → 1100 1001 0111
Therefore (964)10 = (1100 1001 0111)XS-3
Conversion of XS-3 code to equivalent decimal numbers :
• Eg.: Convert (0011 1010 1100)XS-3 to its equivalent decimal number.
XS-3 code → 1010 0011 1100
Decimal equivalent → 0 7 9
Therefore (1010 0011 1100)XS-3 = (709)10
--------------------------------------------------------------------------------------------------------------
Obtain XS-3 equivalent of following numbers:
(a) (235)10 (b) (146)10 (c) (0111 1000)BCD (d) (1001 0011)BCD
(e) (101010)2 (hint: first convert to decimal)
Gray Code
Decimal Binary Gray Code
• Non-weighted code.
0 0000 0000
• It has a very special feature that only
1 0001 0001 one bit will change, each time the
2 0010 0011 decimal number is incremented,
3 0011 0010 therefore also called unit distance code.
4 0100 0110
5 0101 0111 Binary and Gray conversions:
6 0110 0101 • For Gray to binary or binary to Gray
7 0111 0100
conversions let’s understand rules for
Ex-OR
8 1000 1100
(Ex-OR is represented by symbol )
9 1001 1101
10 1010 1111 Rules for EX-OR:
11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000
Conversion from Binary to Gray code:
Step 1: Write MSB of given Binary number as it is.
Step 2: Ex-OR this bit with next bit of that binary number and write the result.
Step 3: Ex-OR each successive sum until LSB of that binary number is reached.
• Eg.: Convert (1010011)2 to its equivalent Gray code.
1 0 1 0 0 1 1
1 1 1 1 0 1 0
Therefore (1010011)2 = (1111010)Gray
Conversion from Gray to Binary:
Step 1: Write MSB of given Binary number as it is.
Step 2: Ex-OR this bit with next bit of that binary number and write the result.
Step 3: Continue this process until LSB of that binary number is reached.
• Eg.: Convert (1010111)Gray to its equivalent Binary number.
1 0 1 0 1 1 1
1 1 0 0 1 0 1
Therefore (1010111)Gray = (1100101)2