Digital Logic and Computer Design
Number Codes and Registers
ENGIN112 L4: Number Codes and Registers
2’s Complement Subtraction
° Let’s compute (13)10 - (5)10.
• (13)10 = +(1101)2 = (01101)2
• (-5)10 = -(0101)2 = (11011)2
° Adding these two 5-bit codes…
0 1 1 0 1
carry + 1 1 0 1 1
--------------
1 0 1 0 0 0
° Discarding the carry bit, the sign bit is seen to be
zero, indicating a correct result.
ENGIN112 L4: Number Codes and Registers
2’s Complement Subtraction
° Let’s compute (5)10 – (12)10.
• (-12)10 = -(1100)2 = (10100)2
• (5)10 = +(0101)2 = (00101)2
° Adding these two 5-bit codes…
0 0 1 0 1
+ 1 0 1 0 0
--------------
1 1 0 0 1
° Here, there is no carry bit and the sign bit is 1.
This indicates a negative result, which is what we
expect. (11001)2 = -(7)10.
ENGIN112 L4: Number Codes and Registers
Binary Coded Decimal
Digit BCD Digit BCD
Code Code
0 0000 5 0101
1 0001 6 0110
2 0010 7 0111
3 0011 8 1000
4 0100 9 1001
° Binary coded decimal (BCD) represents each decimal
digit with four bits
• Ex. 0011 0010 1001 = 32910
3 2 9
° This is NOT the same as 0011001010012
° Why do this? Because people think in decimal.
ENGIN112 L4: Number Codes and Registers
Putting It All Together
BCD is not a very efficient way to represent numbers in binary, as
it requires more bits to represent a number than pure binary
representation. However, BCD was commonly used in
early computers (such as in the 1940s and 1950s) because it
allowed for easy conversion between decimal and binary,
which was important for many applications at the time.
BCD is also commonly used to encode numbers for seven-
segment displays, which are often used in digital clocks,
calculators, and other electronic devices. Each segment in a
seven-segment display can be turned on or off to represent
a digit from 0 to 9, making BCD a convenient way to
represent decimal digits for such displays.
While BCD is not very efficient in terms of storage and
processing, it is still used in certain applications where
human readability is important. Because people are
accustomed to thinking and working with decimal digits,
BCD can be easier to read and understand than pure binary
representation, especially for non-technical users.
ENGIN112 L4: Number Codes and Registers
Gray Code
Digit Binary Gray
Code ° Gray code is not a number
system.
0 0000 0000 • It is an alternate way to represent
four bit data
° Only one bit changes from
1 0001 0001
one decimal digit to the next
° Useful for reducing errors in
2 0010 0011
communication.
° Can be scaled to larger
3 0011 0010 numbers.
4 0100 0110
5 0101 0111
6 0110 0101
ENGIN112 L4: Number Codes and Registers
ASCII Code
° American Standard Code for Information Interchange
° ASCII is a 7-bit code, frequently used with an 8th bit for error
detection (more about that in a bit).
ASCII Character Hex Binary
-------------------------------------
0 NUL 00 00000000
1 SOH 01 00000001
2 STX 02 00000010
3 ETX 03 00000011
4 EOT 04 00000100
5 ENQ 05 00000101
6 ACK 06 00000110
7 BEL 07 00000111
8 BS 08 00001000
9 TAB 09 00001001
10 LF 0A 00001010
11 VT 0B 00001011
12 FF 0C 00001100
13 CR 0D 00001101
14 SO 0E 00001110
15 SI 0F 00001111
16 DLE 10 00010000
17 DC1 11 00010001
18 DC2 12 00010010
19 DC3 13 00010011
ENGIN112 L4: Number Codes and Registers
ASCII Codes and Data Transmission
° ASCII Codes
° A – Z (26 codes), a – z (26 codes)
° 0-9 (10 codes), others (@#$%^&*….)
° Transmission susceptible to noise
° Typical transmission rates (1500 Kbps, 56.6 Kbps)
° How to keep data transmission accurate?
ENGIN112 L4: Number Codes and Registers
Parity Codes
° Parity is used in digital communication systems to detect errors that may occur during
transmission.
° A parity bit is an extra bit that is added to a data word to create a code word. The parity bit
is used to verify the accuracy of the transmission by checking if the number of ones in the
code word is even or odd.
° Parity codes are formed by concatenating a parity bit, P to each code word of C.
° In an odd-parity code, the parity bit is specified so that the total number of ones is odd.
° In an even-parity code, the parity bit is specified so that the total number of ones is even.
P Information Bits
1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 1
Added even parity bit Added odd parity bit
ENGIN112 L4: Number Codes and Registers
Pari
ty
°Cod
e Concatenate a parity bit to the ASCII code for the
characters 0, X, and = to produce both odd-parity
Exa
and even-parity codes.
mpl
e
Character ASCII Odd-Parity Even-Parity
ASCII ASCII
0 0110000 10110000 00110000
X 1011000 01011000 11011000
= 0111100 10111100 00111100
ENGIN112 L4: Number Codes and Registers
Summary
° Although 2’s complement most important, other
number codes exist
° ASCII code used to represent characters (including
those on the keyboard)
° Registers store binary data
ENGIN112 L4: Number Codes and Registers