You are on page 1of 24

Elements of Computing Systems-1

Number systems : (Binary arithmetic, BCD and Gray Code Representations)

Dr. Jyothish Lal G, Assistant Professor (Sr. Gr)


Department of AI / Center for Computational Engineering and Networking (CEN)
Amrita School of AI, Coimbatore
Acknowledgment: Prof. Noam Nisan, Prof. Shimon Schocken
Binary Arithmetic

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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:

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 3


Binary subtraction
• Performed in a similar manner as that of decimal subtraction
• Borrow bits are generated, and used in a similar manner
• Examples:

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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’

• Add the carry back to R (called end-around carry)


• That is, R = R + 1
• The result is a positive number

Else

• The result is negative, and is in 1’s complement form

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 5


Example 1
1’s complement of 2 = 1101

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 6


Example 2
1’s complement of 5 = 1010

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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’

• Ignore the carry


• The result is a positive number

Else

• The result is negative, and is in 2’s complement form

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 8


Example 1
2’s complement of 2 = 1101 + 1 = 1110

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 9


Example 2
2’s complement of 5 = 1010 + 1 = 1011

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 11


Example 1

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 12


Example 2

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 13


Binary coded Decimal(BCD) and Gray
code Representation

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 14


Binary coded Decimal(BCD)
• In BCD,
– Each decimal digit is represented by its 4-bit binary
equivalent
– Conversion is much easier

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 15


EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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:

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 18


EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 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:

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 23


Thank You

EOC-1|B.TECH CSE (AI)|CEN|Dr. Jyothish 24

You might also like