EoC-1_lecture_3__Binary arithmetic, BCD, Gray codes
EoC-1_lecture_3__Binary arithmetic, BCD, Gray codes
Elements of Computing -1
Number systems : (Binary arithmetic, BCD and Gray Code Representations)
28 August 2024 2
Binary addition
• Performed in a similar manner as that of decimal addition
• Corresponding bits are added and if a carry is produced, it is
added to the binary digits at the left
• Examples:
28 August 2024 3
Binary subtraction
• Performed in a similar manner as that of decimal subtraction
• Borrow bits are generated, and used in a similar manner
• Examples:
28 August 2024 4
Subtraction Using Addition :1’s Complement
• How to compute A – B ?
– Compute the 1’s complement of B (say, B1)
– Compute R = A + B1
– If the carry obtained after addition is ‘1’
Else
28 August 2024 5
Example 1
1’s complement of 2 = 1101
28 August 2024 6
Example 2
1’s complement of 5 = 1010
28 August 2024 7
Subtraction Using Addition :2’s Complement
• How to compute A – B ?
– Compute the 2’s complement of B (say, B2)
– Compute R = A + B2
– If the carry obtained after addition is ‘1’
Else
28 August 2024 8
Example 1
2’s complement of 2 = 1101 + 1 = 1110
28 August 2024 9
Example 2
2’s complement of 5 = 1010 + 1 = 1011
28 August 2024 10
Overflow
• When the sign of two numbers are different,
adding them can never result in an overflow
• When does overflow occurs?
– Sign of the two numbers are same
– Sign of the sum is different from the sign of either
of the numbers
28 August 2024 11
Example 1
28 August 2024 12
Example 2
28 August 2024 13
Binary coded Decimal(BCD) and Gray
code Representation
28 August 2024 14
Binary coded Decimal(BCD)
• In BCD,
– Each decimal digit is represented by its 4-bit binary
equivalent
– Conversion is much easier
28 August 2024 15
28 August 2024 16
BCD Addition
• When we add BCD numbers, we may have to go for a correction step where 6
(0110) is added to one of the nibble
– Either when a nibble is one of the six invalid combinations, or there is a carry in from the previous
nibble
• Examples:
28 August 2024 17
Gray codes
❑ Type of non-weighted binary code where successive code
differ in only one bit
▪ Code with this property is also called Cyclic code
❑ Useful in applications that require analog to digital
conversions
▪ To reduce error in conversion
▪ Binary to gray and gray to binary conversions are
easier
28 August 2024 18
28 August 2024 19
Gray code: Self reflecting code
• Let’s say we have gray code representation for m-
bits
• To obtain gray code representation for (m+1)
bits,
– Write the m bit representations, one below the other
as mirror images to each other
– Add 0 at the beginning of every code in the first
group, and 1 at the beginning of every code in the
second group
28 August 2024 20
00 0 00 0 000
01 0 01 0 001
11 0 11 0 011
10 0 10 0 010
1 10 0 110
1 11 0 111
1 01 0 101
1 00 0 100
1 100
1 101
1 111
1 110
1 010
1 011
1 001
1 000
28 August 2024 21
Binary to gray Conversion
Example
b5 b4 b3 b2 b1 b0
1 0 1 1 0 1
+ + + + + +
1 1 1 0 1 1
g5 g4 g3 g2 g1 g0
28 August 2024 22
Gray to binary code conversion
• We start with leftmost bit and proceed to the
LSB, and set
• bi = gi if no. of 1’s preceding gi is even
• bi = gi’ if no. of 1’s preceding gi is odd
Example:
28 August 2024 23
Thank You
28 August 2024 24