Digital Codes
Digital Codes
To illustrate the BCD code, take a decimal number such as 874. Each digit is
changed to its binary equivalent as follows:
8 7 4 Decimal
1
University of Baghdad Number System & Codes
College of Engineering/ Dept. of Computer Engineering Lecture No. 3
Logic By Zainab Hassan
It’s also important to understand that a BCD number is not the same as a straight
binary number. A straight binary code takes the complete decimal number and
represents it in binary; the BCD code converts each decimal digit to binary
individually. To illustrate, take the number 137 and compare its straight binary and
BCD codes.
137(10) = 10001001(2) (Binary)
137(10) = 000100110111 (BCD)
The BCD code requires 12 bits while the straight binary code requires only 8
bits to represent 137. BCD is used in digital machines whenever decimal
information is either applied as inputs or displayed as outputs. Digital voltmeter,
frequency counters, and digital clocks, all use BCD because they display output
information in decimal. BCD is not often used in modern high speed digital
computers for the reason that the BCD code for a given decimal number requires
more bits than the straight binary code and is therefore less efficient. This is
important in digital computers because the number of places in memory where
these bits can be stored is limited.
3 5 9 8 1 7 0 2 4 6 9
0011 0101 1001 1000 0001 0111 0000 0010 0100 0110 1001
10000110 1001010001110000
8 6 9 4 7 0
2
University of Baghdad Number System & Codes
College of Engineering/ Dept. of Computer Engineering Lecture No. 3
Logic By Zainab Hassan
2- Gray Code:
The gray code is un-weighted and is not an arithmetic code; that is, there are
no specific weights assigned to the bit positions. The important feature of the Gray
code is that it exhibits only a single bit change from one code number to the next.
Table (2) is a listing of the four bit gray code for decimal numbers 0 through 15.
Notice the single bit change between successive gray code numbers. For instance,
in going from decimal 3 to decimal 4, the gray code changes from 0010 to 0110,
while the binary code changes from 0011 to 0100, a change of three bits. The only
bit change is in the third bit from the right in the gray code; the other remain the
same.
Table (2)
Decimal Binary Gray Decimal Binary Gray
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
Binary-to-Gray Conversion:
Conversion between binary code and Gray code is sometimes useful. in the
conversion process, the following rules apply:
The most significant bit (left-most) in the gray code is the same as the
10110 (Binary)
1 (Gray)
3
University of Baghdad Number System & Codes
College of Engineering/ Dept. of Computer Engineering Lecture No. 3
Logic By Zainab Hassan
Step 2: add the left-most binary code bit to the adjacent one:
1 + 0 110 (Binary)
1 1 (Gray)
1 0 + 1 10 (Binary)
1 1 1 (Gray)
Step 4: add the next adjacent pair and discard the carry:
1 0 1+10 (Binary)
1 1 1 0 (Gray)
Step 5: add the last adjacent pair:
1 0 1 1+0 (Binary)
1 1 1 0 1 (Gray)
Gray-to-Binary Conversion:
To convert from Gray code to binary, a similar method is used, but there are
some differences. The following rules apply:
The most significant bit (left-most) in the binary code is the same as the
4
University of Baghdad Number System & Codes
College of Engineering/ Dept. of Computer Engineering Lecture No. 3
Logic By Zainab Hassan
Example: convert the Gray code number 11011 to binary.
Step 1: the left-most bits are the same.
11011 (Gray)
1 (Binary)
Step 2: add the last binary code bit just generated to the gray code bit in the next
1 1 011 (Gray)
+
1 0 (Binary)
Step 3: add the last binary code bit generated to the next Gray code bit.
1 1 0 11 (Gray)
+
1 0 0 (Binary)
Step 4: add the last binary code bit generated to the next Gray code bit.
1 1 0 1 1 (Gray)
+
1 0 0 1 (Binary)
Step 5: add the last binary code bit generated to the next Gray code bit. discard
carry.
1 1 0 1 1 (Gray)
+
1 0 0 1 0 (Binary)
5
University of Baghdad Number System & Codes
College of Engineering/ Dept. of Computer Engineering Lecture No. 3
Logic By Zainab Hassan
(a) Binary to Gray code:-
1+1+0+0+0+1+1+0
1 0 1 0 0 1 0 1
1 0 1 0 1 1 1 1
1 1 0 0 1 0 1 0
3- Excess-3 Code:
This is a digital code related to BCD that is derived by adding 3 to each
decimal digit and then converting the result of that addition to 4-bit binary. This
code is un-weighted. For instance, the excess-3 code for decimal 2 and 9 are:
2 9
+3 +3
5 (0101) 12 (1100)
The excess-3 code for each decimal digit is found by the same procedure. The
entire code is shown in Table (3).
Table (3)
6
University of Baghdad Number System & Codes
College of Engineering/ Dept. of Computer Engineering Lecture No. 3
Logic By Zainab Hassan
1 0001 0100
2 0010 0101
3 0011 0110
4 0100 0111
5 0101 1000
6 0110 1001
7 0111 1010
8 1000 1011
9 1001 1100
1 3 4 3 0
+3 +3 +3 +3 +3
4 6 7 6 3
4- Alphanumeric Code:
In order to be very useful, a computer must be capable of handling non-
numeric information. In other words, a computer must be able to recognize codes
that represent numbers, letters, and special characters. These codes are classified as
alphanumeric codes. The most common alphanumeric code, known as the
American Standard Code for Information Interchange (ASCII), is used by most
minicomputer and microcomputer manufacturers.
The ASCII is a seven-bit code in which the decimal digits are represented by
the 8421 BCD code preceded by 011. The letters of the alphabet and other symbols
and instructions are represented by other code combinations, shown in Table (4).
For instance, the letter A is represented by 1000001 (4116), the comma by 0101100
(2C16) and the ETX (end of text) by 0000011 (0316).
7
University of Baghdad Number System & Codes
College of Engineering/ Dept. of Computer Engineering Lecture No. 3
Logic By Zainab Hassan
Table (4)
MSBS
LSBS
000 (0) 001 (1) 010 (2) 011 (3) 100 (4) 101 (5) 110 (6) 111 (7)
0000 NUL DLE SP 0 @ P ` p
0001 SOH DC1 ! 1 A Q a q
0010 STX DC2 “ 2 B R b r
0011 ETX DC3 # 3 C S c s
0100 EOT DC4 $ 4 D T d t
0101 ENQ NAK % 5 E U e u
0110 ACK SYN & 6 F V f v
0111 BEL ETB ‘ 7 G W g w
1000 BS CAN ( 8 H X h x
1001 HT EM ) 9 I Y i y
1010 LF SUB * : J Z j z
1011 VT EXC + ; K [ k {
1100 FF FS , < L \ l
1101 CR GS - = M ] m }
1110 SO RS . > N n ~
1111 SI US / ? O _ o DEL
Example: determine the codes that are entered from the computer’s keyboard
when the following basic program statement is typed in. also express each in
hexadecimal notation.
20 PRINT “A=”;X
8
University of Baghdad Number System & Codes
College of Engineering/ Dept. of Computer Engineering Lecture No. 3
Logic By Zainab Hassan
“ 0100010 22
; 0111011 3B
X 1011000 58
Review Questions:
1. How many bits are required to represents the decimal numbers in the range
from 0 to 999 using straight binary code? Using BCD code?
2. What is the binary weight of each 1 in the following BCD numbers?
(a) 0010 (b) 1000 (c) 0001 (d) 0100
3. Convert the following binary numbers to Gray codes?
(a) 1100 (b) 1010 (c) 11010
4. Convert the following Gray codes to binary?
(a) 1000 (b) 1010 (c) 11101
5. Convert the following decimal numbers to Excess-3 code?
(a) 3 (b) 87 (c) 349
6. Convert decimal 928 to Excess-3?