Binary Arithmatic and Codes
Binary Arithmatic and Codes
2
Digital circuits in practice, are required to handle data which may be in
numeric, alphabets or special characters
Even when the data is in the most familiar decimal form, the conservation
into binary equivalent is lengthy and cumbersome
This requires conversion of the incoming data into binary format in a
simple manner.
Therefore, for simple conversion of data into binary format several types of
binary codes are developed which fall under two broad categories namely,
weighted and non-weighted codes.
3
In any weighted code, the decimal digits 0 to 9 will be represented by 10
distinct code words
Each bit in the code word will be having specific weight
Wn…..W4 W3 W2 W1 Weights
Xn……X4 X3 X2 X1 Binary Code Word
In order to represent the decimal numbers 0 to 9 a minimum of four bits are
required
The corresponding decimal equivalent digit for the binary code X4 X3
X2 X1 with weights W4 W3 W2 W1 is obtained as follows.
N = X4×W4 + X3×W3 + X2×W2 + X1×W1
When an n digit decimal number is to be converted into any weighted code,
each digit in the decimal number will be simply replaced with the
equivalent four-bit code word.
4
Binary Coded Decimal code (BCD) code is also referred to as 8421, since
8, 4, 2 and 1 are the weights of the bits in the code word.
The bits X4 X3 X2 and X1 in the code word will be having weights 8,4,2
and 1 respectively
8 4 2 1 Weights
X4 X3 X2 X1 BCD Code Word
The decimal equivalent N for the BCD code word X4 X3 X2 X1 is
N = 8 × X4 + 4 × X3 + 2 × X2 + 1 × X1 where X1 to X4 can be a 0 or 1
Some Codes can have a combination of positive and negative weights
5
The equivalent decimal number for any code word is simply obtained by
adding the products of all the bits with their corresponding weights.
Example: the decimal equivalent of 1001 in BCD code is 1*8 + 0*4 + 0*2+ 1*1 =9
Decimal w4 w3 w2 w1
Digit 8421 2421 642–3 Invalid code words: the
0 0000 0000 0000 unused code words in a code
1 0001 0001 0101 are called invalid code words.
2 0010 0010 0010 1010, 1011, 1100, 1101, 1110
3 0011 0011 1001 and 1111 are invalid code
4 0100 0100 0100 words in BCD code
5 0101 1011 1011
6 0110 1100 0110
7 0111 1101 1101
8 1000 1110 1010
9 1001 1111 1111
The representations of some decimal numbers in the 2421 and 642 –3 codes are not
unique. For example, 4 can be represented as 1010 or 0100 in 2 4 2 1 code. 6 can
be represented as 1000 or 0110 in 642 –3 code. Such ambiguity is overcome by
adopting representation which makes the code Self complementing code
6
A code is said to be self-complementing code, if the code word of a 9s complement
of the decimal number N, can be obtained by complementing the code word of N.
A code can be Self Complementing Code if and only if the sum of the weights of
the code is exactly equal to 9.
Decimal Code
Digit 2421
0 0000
1 0001
2 1000
3 1001 All the pairs connected by arrows are
4 1010 complements of each other and their
5 0101 decimal equivalent are 9s
6 0110 complements of each other. Such a
7 0111 code is Self -Complementing Code.
8 1110
9 1111
For example, in 2421 code, the code word of the decimal number 3 is 0011.
N= 3 0011
Now, the 9s complement of the decimal number 3 is 9–3=6. The code word
of decimal 6 in 2421 is 1100 from the table. This can be simply be obtained
by complementing the code word of 3 which is 0011.
7
Example: Convert the following decimal numbers to BCD.
1. 8234 → 1000 0010 0011 0100
2. 3421 → 0011 0100 0010 0001
3. Example: Represent the following decimal numbers using 2421 code
4. 5164 → 1011 0001 1100 0100
Example: Convert the following BCD number to decimal
1. (110 0101.1001 01) BCD → 0110 │ 0101 . 1001│ 0100
Example: Convert the following binary number to its equivalent BCD.
10110112=26+0+24+23+0+21+20
= 64+16+8+2+1
= 9110
=1001 0001
8
Unlike weighted codes, the non-weighted codes do not have
any positional weights.
There are two popularly used non-weighted codes
• Excess Code or 3 XS–3 code
• Gray code.
9
The Excess –3 code is also called as XS–3 code. It is obtained by adding a 3, i.e.,
0011 to the code words of BCD. XS–3 code is a 4-bit code.
XS-3 is a non weighted self
complementing code
10
Gray code is a n bit non-weighted code which is obtained from binary code
Bn ⊕ Bn-1 ⊕ Bn-2…⊕…..B3 ⊕ B2 ⊕ B1…….. Binary Code
Gray 1 1 1 0 1 1 0 1 1 0 0 0 1 1 0 1
11
Gray to binary code conversion
Gray Gn Gn 1 Gn2 …. G2 G1
Binary Bn Bn 1 Bn2 …. B2 B1
Binary 1 0 0 1 0 0 1 0
12
Decimal Binary Gray
Distance: The distance between
(4 Bit) (4 Bit)
any two code words is defined as
0 0000 0000 the number of positions in which
1 0001 0001 the two code words differ.
2 0010 0011
3 0011 0010
Cyclic Code: A code in which
4 0100 0110
each code word differs from the
5 0101 0111
successive or preceding code
6 0110 0101
word only by one position is
7 0111 0100
called as Cyclic code.
8 1000 1100
9 1001 1101 Gray code is a Cyclic code
10 1010 1111
11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000
13
𝟐 𝐛𝐢𝐭 𝟐 𝐛𝐢𝐭 𝟑 𝐛𝐢𝐭 𝟒 𝐛𝐢𝐭
𝐁𝐢𝐧𝐚𝐫𝐲 𝐆𝐫𝐚𝐲 𝐆𝐫𝐚𝐲 𝐆𝐫𝐚𝐲
00 00 000 0000
Reflecting Code : A code is
01 01 001 0001
called Reflecting Code if ‘n’ bit
10 11 011 0011
code can be obtained by
11 10 010 0010 reflecting (n-1) bit code.
110 0110
111 0111
101 0101 Gray code is also a reflecting
100 0100 code
1100
1101
1111
1110
1010
1011
1001
1000
14
BCD numbers are added applying binary addition. However, after addition if any of
the nibbles in the resultant sum is invalid, ie greater than 9 (1001) or a carry is
generated from that stage, then 6 (0110) must be added to the sum to bring the
result to valid form
Assignment:
(i) Add 476 and 945 in BCD
(ii) Add 734 and 98 in BCD
15
BCD numbers are subtracted applying binary subtraction. However, after
subtraction if any of the nibbles in the resultant difference is invalid, ie greater than
9 (1001) or a borrow is taken by that stage, then 6 (0110) must be subtracted from
the difference to bring the result to valid form
Assignment:
(i) Subtract 976 and 595 in BCD
(ii) Subtract 734 and 98 in BCD
16
Self learning question :
How many self complementing codes exist with positive
weights?
17
Outcome of the Lecture
18
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
KAKATIYA INSTITUTE OF TECHNOLOGY & SCIENCE, WARANGAL
(An Autonomous Institute under Kakatiya University, Warangal)
Assignment- WEEK-2
(UNIT – I)
V.Raju
Assistant Professor
Dept. of ECE
Thank you!!!!!