A computer stores a letter or a decimal digit as a pattern of bits. A code is an agreed set of bit patterns, one for each symbol. Different codes are used for different purposes: some for easy decimal arithmetic, some for reliable counting circuits and some for text.
Binary Coded Decimal and Excess-3
In BCD each decimal digit from 0 to 9 is coded in four bits using its ordinary binary value, so 259 is 0010 0101 1001. The Excess-3 code is obtained by adding 3 to each decimal digit before it is written in four bits. It is a self-complementing code: the 9's complement of a digit is found by inverting its four bits. BCD is easy to read and convert, but it wastes some capacity, because four bits can hold sixteen patterns while only ten are used.
Definition: Binary coded decimal. A code in which every decimal digit is represented separately by its four-bit binary equivalent.
Table 1.9: BCD and Excess-3 codes
| Decimal | BCD | Excess-3 |
|---|---|---|
| 0 | 0000 | 0011 |
| 1 | 0001 | 0100 |
| 2 | 0010 | 0101 |
| 5 | 0101 | 1000 |
| 9 | 1001 | 1100 |
The Gray Code
The Gray code is a code in which two consecutive numbers differ in exactly one bit. It is used in position encoders, where a change of only one bit avoids false readings. To convert binary to Gray, the leftmost bit is copied and each following Gray bit is the XOR of two adjacent binary bits. Binary 1011 becomes Gray 1110.
Character Codes
ASCII is a seven-bit code that represents 128 characters, including letters, digits and control codes. The letter A is 65 (1000001), a is 97 and the digit 0 is 48. EBCDIC is an eight-bit code used on IBM mainframe computers. In EBCDIC the letter A is C1 and the digit 0 is F0 in hexadecimal. Text is stored by these code values, so the word BCA is held as the three ASCII values 66, 67 and 65. Since every character has a fixed number, letters can also be compared and sorted by their codes.
Example 1.10: Coding a number in different codes
Problem: Write the decimal number 259 in BCD and Excess-3, and write binary 1011 in Gray code.
Solution:
- BCD: 2 is 0010, 5 is 0101 and 9 is 1001, giving 0010 0101 1001.
- Excess-3: add 3 to each digit to get 5, 8 and 12, which are 0101, 1000 and 1100.
- Gray: keep the first bit 1, then 1 XOR 0 = 1, 0 XOR 1 = 1, 1 XOR 1 = 0.
Result: BCD 0010 0101 1001, Excess-3 0101 1000 1100 and Gray 1110.
for d in "259":
print(d, format(int(d), "04b"), format(int(d) + 3, "04b"))
b = 0b1011
print(format(b ^ (b >> 1), "04b"), ord("A"), format(ord("A"), "07b"))
Output
2 0010 0101
5 0101 1000
9 1001 1100
1110 65 1000001
Common mistakes
- Using BCD codes 1010 to 1111, which are not valid digits.
- Converting the whole decimal number to binary when BCD needs each digit coded separately.
- Mixing up ASCII (seven bits) with EBCDIC (eight bits).
Key takeaways
- BCD codes each decimal digit in four bits.
- Excess-3 equals BCD plus three.
- Gray code changes only one bit between consecutive values.
- ASCII uses seven bits; EBCDIC uses eight.
Practice questions
- What is Excess-3 code of decimal 4?
- Why is the Gray code used in position encoders?
- How many characters can ASCII represent?
Answers to the practice questions
- 4 + 3 = 7, which is 0111.
- Only one bit changes between consecutive positions, so a misread is at most one step wrong.
-
Sources
- Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit I
