Binary Number System,
Binary Addition/Subtraction
Sung Soo Hwang
Binary Number System
• Decimal number system
7642 = 764210 = 7 × 103 + 6 × 102 + 4 × 10 + 2
• Binary number system
1011112 = 1 × 25 + 0 × 24 + 1 × 23 + 1 × 22 + 1 × 21 + 1
= 32 + 8 + 4 + 2 + 1 = 47 = 4710
Converting Decimal Number to Binary
• 1st method
•
•
•
•
1. Subtract the number the largest power of 2 less than that number.
2. Put 1 in the corresponding position of the binary equivalent.
3. Repeat that with the remainder.
4. 0 is put in the position for those powers of 2 that are larger than
the remainder.
• 2nd method
• 1. Divide the decimal number by 2 repeatedly.
• 2. The remainder at each time gives a digit of the binary answer,
starting at the least significant bit.
Converting Decimal Number to Binary
• Example with 105
• 1st method
• 105- 64(26 ) = 41
• 41 – 32(25 )= 9
• 9 – 8(23 )= 1
• 1 – 1(20 )= 0
• β1101001
π
ππ
0
1
1
2
2
4
3
8
4
16
• 2nd method
• 105 / 2 = 52(remainder:1)
• 52/2 = 26(remainder:0)
• 26/2 = 13(remainder:0)
• 13/2 = 6(remainder:1)
• 6/2 = 3(remainder:0)
• 3/2 = 1(remainder:1)
• 1/2 = 0(remainder:1)
• β1101001
5
32
6
64
7
128
8
256
9
512
10
1024
11
2048
12
4096
13
8192
Binary Codes
• Binary coded decimal(BCD)
• They are necessary to represent a single decimal digit
• How many bits are required to represent decimal digit? β 4 bits
• Weighted codes
• The value represented is computed by taking the sum of each
digit times its weight
Decimal digit
8421 code
5421 code
2421 code
0
0000
0000
0000
1
0001
0001
0001
2
0010
0010
0010
3
0011
0011
0011
4
0100
0100
0100
5
0101
1000
1011
6
0110
1001
1100
7
0111
1010
1101
8
1000
1011
1110
9
1001
1100
1111
Binary Codes
• Gray code
• Consecutive numbers differ in only one bit
Decimal digit
Gray code
Decimal digit
Gray code
0
0000
10
1111
1
0001
11
1110
2
0011
12
1010
3
0010
13
1011
4
0110
14
1001
5
0111
15
1000
6
0101
7
0100
8
1100
9
1101
Binary Addition
• Binary addition
1
carry
1
0
+0
0
0
+1
1
1
+1
10
1
1
+1
11
sum
sum
sum
sum
carry
• Binary addition on each bit generates carry and sum
• For multi-digit binary addition, start from LSB
• LSB: Least significant bit
• MSB: Most significant bit
11
1010
+ 0111
10001
carry
Two’s Complement Format
• Computers must deal with signed numbers
• Signed binary numbers are nearly always stored in two’s
complement format
• The leading bit is the sign bit (0 for positive)
• Positive number
• The largest number that can be stored is 2n-1-1
• In a 4-bit system, +5 would be stored as 0101
• Negative number
• -a is stored as the binary equivalent of 2n –a in a n-bit system
• In a 4-bit system, -3 is stored as the binary for 16-3 = 13, 1101
• The most negative number that can be stored is -2n-1
Two’s Complement Format
• Finding the storage format for negative numbers
• 1. Find the binary equivalent of the magnitude
• 2. Complement each bit
• 3. Add 1
• Example
• -5 β 5 β 0101 β 1010 β 1010 +1 = 1011
• 0101 β 1010 β1010+1 = 1011
• 1011 β 0100 β0100+1 = 0101
Binary Subtraction
• Binary Subtraction
• 1. Taking the two’s complement of the second operand
• 2. Perform addition
0010
- 0111
Two’s complement format for 0111 β 1000 β 1001
0010
+ 1001
1011
Ignore this bit! (Use it for checking overflow)
Overflow
• Definition
• When addition or subtraction produces a result that exceeds the
range of the number system
• Overflow in unsigned number system
0001
0110
+ 0111
0101
0110
+ 0111
1001
0110
+ 0111
1001
0110
+ 0111
0001
0001
1101
+0101
0001
0101
1101
1101
+0101
0001
10010
Overflow when the end carry is 1
Binary addition/subtraction
• Overflow in signed number system
• The computation of 7-5
• -5 → 1011
0111
+1011
(1)0010
Overflow?
• Overflow occurs when the signs of addends are the same and the
sign of the sum is different
• pos.+pos. =neg. or pos. - neg. =neg.
• neg.+neg. =pos. or neg. - pos. =pos.
βCarry-in to sign bit differs from carry-out
• Addition of two numbers with different signs can never produce
overflow