11 Binary Codes
11 Binary Codes
Introduction
• Digital systems use signals that have two distinct values and circuit elements that have two stable
states.
• Digital systems represent and manipulate not only binary numbers, but also many other discrete
elements of information.
• Any discrete element of information distinct among a group of quantities can be represented by a
binary code.
• Binary codes play an important role in digital computers.
• The codes must be in binary because computers can only hold 1's and 0's.
• An n bit code is a group of ‘n’ bits that assume upto 2n distinct combinations of 1’s and 0’s,with
each combination representing one element of the set that is being coded. (2bits - 4 elements, 3
bits- 8 elements)
• The bit combination of an n-bit code varies from the count in binary from 0 to 2n - 1.
• Each element must be assigned a unique binary bit combination and no two elements can have
the same value
2
Classification of binary codes
Weighted binary codes
Decimal 8421 2421
• Each position of the number represents a specific 0 0000 0000
weight
1 0001 0001
• Eg: 8421,2421,5211 codes 2 0010 0010
3 0011 0011
Reflective codes 4 0100 0100
• When the code for 9 is the complement for a code 5 0101 1011
0, 8 for 1, 6 for 3 and 5 for 4. 6 0110 1100
• Eg: 2421,5211 and XS3 codes 7 0111 1101
8 1000 1110
Sequential codes 9 1001 1111
• When each succeeding code is one binary number
greater than its preceding code.
• Eg: 8421 and XS3 codes 3
BCD(Binary Coded Decimal) code
• Representation of decimal numbers/digits simply by encoding each digit in binary form(contains 1's
and 0's) – (Binary-Coded-Decimal (BCD) representation.
• It is possible to perform the arithmetic operations directly on decimal numbers when they are stored
in the computer in BCD form
• For the 4 bit combinations, only 10 of the 16 available patterns(0,1,2,3,4,5,6,7,8,9) are used in BCD
• The remaining 6 patterns should not occur in logic circuits that operate on BCD operands
• The unused patterns (1010 to 1111 ie 10,11,12,13,14,15) are usually treated as don’t-care
conditions.
• Used in early computers and handheld calculators for digital oriented display
• Drawbacks : More complexity and six of the possible code patterns are wasted.
• A number with k decimal digits will require 4K bits in BCD code.
• Eg: (398)10 = (0011 1001 1000)BCD (3 digits in decimal) = (4*3=12 bits in BCD)
• Convert (4738)10 into BCD code.
(4738)10 (0100 0111 0011 1000)BCD
4
BCD Arithmetic operations
• BCD numbers require more bits than equivalent binary representation
• BCD numbers are decimal numbers and not binary numbers
(15)10 (0001 0101)BCD (1111)2
BCD Addition
Case 1
Addition of two binary numbers Addition of two BCD digits if the sum is less than 9
• Operands: unsigned X and Y if X +Y ≤ 9, then the addition is the same as the addition of 2 four-bit unsigned
• X = x 3 x2 x1 x0 binary numbers.
• Y = y 3 y2 y1 y0 if X + Y > 9, then the result requires two BCD digits.
• S=X+Y
• S = s 3s2s1s0 0100
4
• Result: Sum(S) and Carry
+ 5 + 0101
9 1001
5
BCD Addition
Case 2 when the sum is greater than 9 and no carry-out is generated using 4 bits.
Addition of two BCD digits is needed correction if the sum exceeds 9
4 0100
Invalid BCD Z=X+Y
+ 9 + 1001
correction is required If Z ≤ 9, then S = Z and carry-out = 0
13 1101 if Z > 9, then X = Z + 6 and carry-out = 1
+ 0110 Add 6 to Sum
1 0011 Sum = 13
Case 3 when the sum is greater than 15 and a carry-out is generated using 4 bits
8 1000
Sum > 15
+ 9 + 1001
correction is required
17 1 0001
+ 0110 Add 6 to Sum
1 0111 Sum = 17
6
Examples
Example 1 Add 647 and 492 in 8421 BCD code
BCD carry 1 1
647 0110 0100 0111
X
Y + 492 + 0100 1001 0010
Binary Sum 1139 1010 1101 1001
Add 6 to Sum + 0110 0110
1139 1 0001 0011 1001
Example 2 Add 9648 and 3438 in 8421 BCD code
BCD carry 1 1 1
9648 1001 0110 0100 1000
X
Y + 2439 + 0010 0100 0011 1001
Binary Sum 12087 1011 1010 0111 10001
Add 6 to Sum + 0110 0110 0110
12087 1 0010 0000 1000 0111
7
Non weighted code
Decimal 8421 XS3 code
• Codes that are not positionally
0 0000 0011
weighted(each position is not assigned
with fixed value) 1 0001 0100
2 0010 0101
• Eg: Excess-3 (XS3) code
3 0011 0110
• ES3 code for binary value =
corresponding 8421 code + 3 4 0100 0111
5 0101 1000
6 0110 1001
7 0111 1010
8 1000 1011
9 1001 1100
https://www.tutorialspoint.com/what-is-excess-3-code
8
Gray code
Continuous or analog information is converted into digital form by means of an Analog- Digital
converter(ADC)
Convenient to use the Gray code to represent the digital data when it is converted from analog
data
The advantage of the Gray code: Only one bit in the code group changes when the decimal
number is incremented from one number to the next.
But binary number changes more than one bit even 4 bits (from 7 to 8 ie. 0111 to 1000)
If binary numbers are used, a change from 0111 to 1000 may produce an intermediate erroneous
number 1001 if the rightmost bit takes more time to change than the other three bits.
Gray code eliminates this problem since only one bit changes in value during any transition
between two numbers
Gray code is used to reduce the error but not in arithmetic operations
Can be used to in to minimize a logic circuit
Better for error minimization in converting analog signals to digital signals
9
Decimal Gray Binary
Application 0 0000 0000
• To find the position in rotary encoders 1 0001 0001
• Gray code is used to represent the analog data by continuous change of
2 0011 0010
a finding position in rotary encoder shaft position.
• The shaft is partitioned into segments, and each segment is assigned a 3 0010 0011
number. 4 0110 0100
• If adjacent segments are made to correspond with the Gray-code
5 0111 0101
sequence, ambiguity is eliminated when detection is sensed in the line
that separates any two segments. 6 0101 0110
7 0100 0111
011 001 8 1100 1000
010 001
000 9 1101 1001
011 000 010
10 1111 1010
11 1110 1011
⨁ ⨁ ⨁
Convert a binary 1011 to gray code
Binary 1 0 1 1
Gray code - 1110
Gray 1 1 1 0
1 1 0 1 1 1 0
Gray Gray code – 1101110 11
Gray code to Binary code conversion
Gray code to Binary code
1. Record the MSB bit of gray code
2. Add the binary MSB to the bit to the next significant bit of the gray code
3. Record the sum and neglect the carry occurred
4. Continue the process until the LSB is reached.
Binary 1 1 1 0 0 1 0
Binary code – 1110010 12
ASCII Character Code
• Digital computers require the handling not only of numbers, but also of other characters or symbols such as
the letters of the alphabets.
• Binary code must represent numerals and special characters such as $.
• An alphanumeric character set is a set of elements that includes the 10 decimal digits, the 26 letters of the
alphabet, and a number of special characters.
• The standard binary code for the alphanumeric character is the American Standard Code for Information
Interchange (ASCII) which uses seven bits to code 128 characters
• Seven bits of the code designated by b1 through b7. (b7-MSB and b1-LSB)
• ASCII code for the Letter B is ( b7b6b5 b4b3b2b1 = 1000010) ( column 100 and row 0010)
• ASCII code contains 94 graphic characters that can be printed and 34 non printing characters used for various
control functions.
13
ASCII code Table
14
Control characters
• The graphic Characters consist of the 26 uppercase letters (A through Z), the 26 lowercase letters
(a through z). the 10 numerals (0 through 9) and 32 special printable characters such as ( &,+,#.
*,. and %).
• 34 control characters are designated in the ASCII table with abbreviated names
• The control characters are used for routing data and arranging the printed text into a prescribed
format.
16
ASCII code Control Characters Table
17
Error detecting code
• To detect errors in data communication and processing, an eighth bit is sometimes added to the
ASCII character to indicate its Parity
• A parity bit is an extra bit included with a message to make the total number of 1's either even or
odd.
• Total no of 1’s in the entire message (word including parity bit) is odd odd Parity
• Total no of 1’s in the entire message (word including parity bit) is even even Parity
18
Applications
• The parity bit is helpful in detecting error during the transmission of information from one
location to another.
• Even Parity bit is commonly used
• The even parity bit is generated by parity generator in sending end for each character and the 8
bit character include parity bit is transmitted to the destination.
• The Parity of each character is checked by parity checker in the receiver end.
• If the parity of the received character is not even, the receiver detects the error ie at least one bit
has changed during transmission
• This code detects only one, three or any odd combinations of error of each character(mostly
suitable for single bit error) and can’t detect even combination of error.
• If the transmitted message 10101010 and received message 10011010 even error occurred
can’t detected by the parity code
• Other suitable error detection code such as check sum etc can be used
19
Acknowledgement of errors
• If receiver detects a parity error, it sends back ASCII NAK (negative acknowledge) control
character with even parity bits(10010101)
• If no error detects, receiver sends back an ACK (acknowledge) control character
(00000110)
• The sending end will respond for NAK and retransmit the message until the correct parity
is detected
• If the error occurs after a many number of attempts, a message can be sent to the
operator to check for malfunctions in the transmission path.
20