Binary Codes
Binary codes are of two types:
 Weighted Codes.
 Non weighted or Unweighted Codes.
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
Examples of Weighted Codes
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.
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.
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.
BCD Subtraction
Non weighted codes
• There are many nonweighted binary codes, two of which are
–Excess-3
–Gray Code
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.
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:
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.
 Example: For a 3-bit Gray code, n=2.
2-bit Gray code:
3-bit Gray code:
0
0
0
0
00
01
11
10
00
01
11
10
1
1
1
1
10
11
01
00
000
001
011
010
110
111
101
100
Gray Code:
Decimal Binary Gray Code
0 0000 0000
1 0001 0001
2 0010 0011
3 0011 0010
4 0100 0110
5 0101 0111
6 0110 0101
7 0111 0100
Decimal Binary Gray Code
8 1000 1100
9 1001 1101
10 1010 1111
11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000
Gray code is a reflective code or mirror image code.
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
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
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)
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
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
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
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
1’s Complement Subtraction
There are Two cases:
• Subtraction of Smaller number from larger number.
• Subtraction of larger number from smaller number.
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
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
2’s Complement Subtraction
There are Two cases:
• Subtraction of Smaller number from larger number.
• Subtraction of larger number from smaller number.
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
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
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
Conclusions
Binary Codes
Weighted Codes(8-4-2-1,2-4-2-1,6-4-2- -3 codes)
Unweighted Codes( Excess-3,Cyclic,Gray codes)
Code conversions (Binary to Gray, Gray to Binary)
 1’s compliment and 2’s compliment
 Boolean addition and Subtraction

Unit-1 (DLD) Lecture 2.pptx

  • 1.
    Binary Codes Binary codesare of two types:  Weighted Codes.  Non weighted or Unweighted Codes.
  • 2.
    Weighted codes • Eachbinary 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
  • 3.
  • 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: • Addcorresponding 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.
  • 7.
    BCD Subtraction: • Subtractcorresponding 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.
  • 8.
  • 9.
    Non weighted codes •There are many nonweighted binary codes, two of which are –Excess-3 –Gray Code
  • 10.
    Excess-3 Code • Itis 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 aGray 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: Fora 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.
  • 13.
     Example: Fora 3-bit Gray code, n=2. 2-bit Gray code: 3-bit Gray code: 0 0 0 0 00 01 11 10 00 01 11 10 1 1 1 1 10 11 01 00 000 001 011 010 110 111 101 100
  • 14.
    Gray Code: Decimal BinaryGray Code 0 0000 0000 1 0001 0001 2 0010 0011 3 0011 0010 4 0100 0110 5 0101 0111 6 0110 0101 7 0111 0100 Decimal Binary Gray Code 8 1000 1100 9 1001 1101 10 1010 1111 11 1011 1110 12 1100 1010 13 1101 1011 14 1110 1001 15 1111 1000 Gray code is a reflective code or mirror image code.
  • 15.
    Binary to Graycode 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 Binarycode 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 toperform 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 aretwo 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’scomplement 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 • The2’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’sand 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 Thereare Two cases: • Subtraction of Smaller number from larger number. • Subtraction of larger number from smaller number.
  • 23.
    Subtraction of Smallernumber 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 Largernumber 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 Thereare Two cases: • Subtraction of Smaller number from larger number. • Subtraction of larger number from smaller number.
  • 26.
    Subtraction of Smallernumber 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 Largernumber 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
  • 30.
    Binary Addition, Subtraction •Negativenumbers must be in 2’s compliment form •If the sum obtained after the addition is negative, it is in 2’s compliment form
  • 31.
    Conclusions Binary Codes Weighted Codes(8-4-2-1,2-4-2-1,6-4-2--3 codes) Unweighted Codes( Excess-3,Cyclic,Gray codes) Code conversions (Binary to Gray, Gray to Binary)  1’s compliment and 2’s compliment  Boolean addition and Subtraction