2. Weighted codes
• Each binary bit is assigned a “Decimal weight”.
8 4 2 1
Ex: (0 1 0 1)2= 510
• Each group of 4 bits, the sum of the weights of those binary bits whose
value is 1 is equal to the decimal number which they represent.
Weighted Codes
• 1.Positive Weighted Code 2.Negative Weighted Code
• Examples:
– 8421 --642-3
– 2421 --84-2-1
– 5211
– 5421
4. BCD(Binary Coded Decimal) CODE
• It is a weighted code.
• It is a special code used to represent single decimal digit.
• Each decimal digit from 0 to 9 is coded by a 4-bit binary number.
• It has six invalid states 1010, 1011, 1100, 1101, 1110 and 1111.
5. BCD Addition:
• Add corresponding bits of two BCD numbers.
• After Addition,
– If Result is <=9 & Carry=0, no correction is needed.
– Result is > 9 or Carry=1, correction is needed.
• then 610 (0110) is added and the resulting carry is added to the next group.
6.
7. BCD Subtraction:
• Subtract corresponding bits of two BCD numbers.
• In Subtraction
– If no borrow from the next higher group then no correction is needed.
– If a borrow from the next group, correction is needed.
• then 610 (0110) is subtracted from the difference term of that group.
9. Non weighted codes
• There are many nonweighted binary codes, two of which are
–Excess-3
–Gray Code
10. Excess-3 Code
• It is self-complementing code.
• It is obtained by adding 3(0011) to 4-bit pattern of the BCD CODE.
• It has six invalid states 0000, 0001, 0010, 1101, 1110 and 1111.
11. Gray Code
In a Gray code only one bit change between each pair of successive
code words So, it is called as Cyclic Code
It can be defined recursively using the following rules:
A 1-bit Gray code has two code words, 0 and 1.
0
1
1-bit Gray code:
12. 0
1
Example: For a 2-bit Gray code, n=1.
1-bit Gray code:
0
1
1
0
0
0
2-bit Gray code:
1
1
00
01
11
10
The first 2n code words of an (n+1)-bit gray code equal the code words
of an n-bit gray code, written in order with a leading 0 appended.
The last 2n code words of an (n+1)-bit Gray code equal the code words
of an n-bit gray code, but written in reverse order with a leading 1
appended.
15. Binary to Gray code conversion
• Binary-to-Gray code conversion
– The MSB in the Gray code is the same as corresponding MSB in the
binary number.
– Going from left to right, add or Ex-OR each adjacent pair of binary code
bits to get the next Gray code bit. Discard carries.
ex: convert 101102 to Gray code
1 + 0 + 1 + 1 + 0 binary
1 1 1 0 1 Gray
16. Gray to Binary code Conversion
• Gray-to-Binary Conversion
– The MSB in the binary code is the same as the corresponding bit in the Gray code.
– Add or Ex-OR each binary code bit generated to the Gray code bit in the next adjacent
position. Discard carries.
Ex: convert the Gray code word 11011 to binary
1 1 0 1 1 Gray
+ + + +
1 0 0 1 0 Binary
17. Complements
• Used to perform subtraction operation (Complement of the
subtrahend is added to the minued).
• It reduces hardware (only adding circuits are needed, Instead of
having separte digital circuits for addition and subtraction)
18. Complements
• There are two types of compliments for each number base system.
–r’s Complement
• r’s complement of N = (rn )-N.
–(r-1)’s Complement
• (r-1)’s complement of N = (rn -1)-N.
Binary(r=2) Decimal(r=10) Octal(r=8) Hexa(r=16)
2's Complement 10's Complement 8's Complement 16's Complement
1's Complement 9's Complement 7's Complement 15's Complement
19. 1’s Complement
• 1’s complement of N = (2n -1)-N.
–If n=4 (2n -1)=10000 - 1 = 1111
• Subtract any 4-bit binary number from 1111.
• This is just inverting each bit.
• Example: 1’s compliment of 1011001
is 0100110
20. 2’s complement
• The 2’s complement of N= 2n-N
= (2n-1)-N+1
= The 1’s complement of N+1
• Example:
–The 1’s complement of 6(0110) is 1001
–The 2’s complement of 6(0110) is 1010
21. Obtain the 1’s and 2’S complements of the following binary numbers :
(a)11101010 (b)01111110 (c)00000001 (d)10000000
1’s complements:
(a) 00010101 (b)10000001 (c)11111110 (d)01111111
2’s complement :
(a) 00010110 (b)10000010 (c)11111111 (d)10000000
22. 1’s Complement Subtraction
There are Two cases:
• Subtraction of Smaller number from larger number.
• Subtraction of larger number from smaller number.
23. Subtraction of Smaller number from larger number
Procedure:
1. Determine the 1’s complement of the smaller number.
2. Add the 1’s complement to the larger number.
3. Remove the carry and add it to result.
Example: Subtract 101011 2 from 111001 2
24. Subtraction of Larger number from Smaller number
Procedure:
1. Determine the 1’s complement of the larger number.
2. Add the 1’s complement to the smaller number.
3. Answer is in 1’s complement form. Take 1’s complement form
and take negative sign
Example: Subtract 111001 2 from 101011 2
25. 2’s Complement Subtraction
There are Two cases:
• Subtraction of Smaller number from larger number.
• Subtraction of larger number from smaller number.
26. Subtraction of Smaller number from larger number
Procedure:
1. Determine the 2’s complement of the smaller number.
2. Add the 2’s complement to the larger number.
3. Discard the Carry.
Example: Subtract 101011 2 from 111001 2
27. Subtraction of Larger number from Smaller number
Procedure:
1. Determine the 2’s complement of the larger number.
2. Add the 2’s complement to the smaller number.
3. Answer is in 2’s complement form. Take 2’s complement form
and take negative sign
Example: Subtract 111001 2 from 101011 2
28.
29.
30. Binary Addition, Subtraction
•Negative numbers must be in 2’s compliment form
•If the sum obtained after the addition is negative, it is in 2’s compliment form