Download presentation
Presentation is loading. Please wait.
1
Addition and Subtraction
2
Learning Objectives Understand 2’s complement system is the most efficient to perform addition and subtraction Learn to draw logic circuits Way to detect overflow
3
Basic Idea Hardware can only deal with binary digits, 0 and 1.
Must represent all numbers, integers or floating point, positive or negative, by binary digits, called bits. Devise electronic circuits to perform arithmetic operations (add, subtract, multiply and divide) on binary numbers.
4
Positive Integers = 32 + 8 +1 = 41
Decimal system: made of 10 digits, {0,1,2, , 9} 41 = 4× ×100 255 = 2× × ×100 Binary system: made of two digits, {0,1} = 0× ×26 + 1×25 + 0× ×23 + 0× ×21 + 1×20 = = 41 = 255, largest number with binary digits, 28-1
5
Writing Numbers 2 5 5 0 0 1 0 1 0 0 1 Most significant digit
Most significant digit Least significant digit Least significant bit (LSB) Most significant bit (MSB)
6
Base or Radix For decimal system, 10 is called the base or radix.
Decimal 41 is also written as 4110 or 41ten Base (radix) for binary system is 2. Thus, 41ten = or two Also, 111ten = two and two = 7ten
7
Signed Integers Use left-most bit (called most significant bit or MSB) for sign: 0 for positive 1 for negative Example: +18ten = two - 18ten = two
8
Why Not Use Sign Bit Sign and magnitude bits should be differently treated in arithmetic operations. Addition and subtraction require different logic circuits. Overflow is difficult to detect. “Zero” has two representations: +0ten = two - 0ten = two Signed-integers are not used in modern computers.
9
Integers With Sign – Other Ways
Use fixed-length representation, but no sign bit 1’s complement: To form a negative number, complement each bit in the given number. 2’s complement: To form a negative number, start with the given number, subtract one, and then complement each bit, or first complement each bit, and then add 1. 2’s complement is the most preferred representation.
10
1’s-Complement To change the sign of a binary integer simply complement (invert) each bit. Example: 3 = 0011, -3 = 1100 n-bit representation: Negation is equivalent to subtraction from 2n – 1 Infinite universe -3 0 3 0 3 6 Modulo-16 universe
11
2’s-Complement Add 1 to 1’s-complement representation.
Some properties: Only one representation for 0 Exactly as many positive numbers as negative numbers Slight asymmetry – there is one negative number with no positive counterpart
12
Three Representations
Sign-magnitude 000 = +0 001 = +1 010 = +2 011 = +3 100 = - 0 101 = - 1 110 = - 2 111 = - 3 1’s complement 000 = +0 001 = +1 010 = +2 011 = +3 100 = - 3 101 = - 2 110 = - 1 111 = - 0 2’s complement 000 = +0 001 = +1 010 = +2 011 = +3 100 = - 4 101 = - 3 110 = - 2 111 = - 1 (Preferred)
13
2’s Complement Numbers 1 2 3 -1 -2 -3 -4 000 001 010 011 100 101 110
1 2 3 -1 -2 -3 -4 000 001 010 011 100 101 110 111
14
2’s Complement n-bit Numbers
Range: - 2n-1 through 2n-1- 1 Unique zero: Expansion of bit length: stretch the left-most bit all the way, e.g., is still – 3. Overflow rule: If two numbers with the same sign bit (both positive or both negative) are added, the overflow occurs if and only if the result has the opposite sign. Subtraction rule: for A – B, add – B to A.
15
Converting 2’s Compliment to Decimal
an-1an a1a0 = -2n-1an-1 + Σ 2i ai i=0 8-bit conversion box Example = = -3
16
32-bit Integers What is the range of integers (positive and negative) that can be represented? Positive integers: 0 to 2,147,483,647 Negative integers: - 1 to - 2,147,483,648 What are the binary representations of the extreme positive and negative integers? = = 2,147,483,647 = = - 2,147,483,648 What is the binary representation of zero?
17
Addition Adding bits: Adding integers: carry 1 1 0
0 + 0 = 0 + 1 = 1 + 0 = 1 + 1 = (1) 0 Adding integers: carry two = 7ten two = 6ten = (1) 1 (1)0 (0)1 two = 13ten
18
Subtraction Direct subtraction Two’s complement subtraction
two = 7ten two = 6ten = two = 1ten two = 7ten two = - 6ten = (1) 0 (1) 0 (0)1 two = 1ten
19
Overflow: An Error -2-3 = -5 110 = -2 3+2 = 5 011 = 3
Examples: Addition of 3-bit integers (range - 4 to +3) -2-3 = = -2 = -3 = = 3 (error) 3+2 = = 3 010 = 2 = 101 = -3 (error) Overflow rule: If two numbers with the same sign bit (both positive or both negative) are added, the overflow occurs if and only if the result has the opposite sign. 000 111 001 1 -1 010 2 110 -2 -3 3 011 101 -4 100
20
Adders
21
Addition of Unsigned Numbers – Half Adder
22
Addition and Subtraction of Signed Numbers
x y Carry-in c Sum s Carry-out c i i i i i +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 s = x y c + x y c + x y c + x y c = x Å y i i i i i i i i i i i i i i i Å c i c = y c x c x y + + i +1 i i i i i i E xample: X 7 1 1 1 x Carry-out i Carry-in + Y = + 6 = + 1 1 y 1 1 c i c i +1 i Z 13 1 1 1 s i Legend for stage i Logic specification for a stage of binary addition.
23
Addition and Subtraction of Signed Numbers
A full adder (FA) y i c x i i x y s i c i i c i + 1 i c i x x y i i i y i c Full adder c i + 1 (F A) i s i (a) Logic f or a single stage
24
Addition and Subtraction of Signed Numbers
n-bit ripple-carry adder x y x y x y n - 1 n - 1 1 1 c c n - 1 1 c n F A F A F A c s s s n - 1 1 Most significant bit Least significant bit (MSB) position (LSB) position (b) An n -bit r ipple-carr y adder
25
Addition and Subtraction of Signed Numbers
kn-bit ripple-carry adder x y x y x y x y x y k n - 1 k n - 1 2 n - 1 2 n - 1 n n n - 1 n - 1 c n - bit n - bit n n - bit c c k n adder adder adder s s s s s s k n - 1 ( k - 1 ) n 2 n - 1 n n - 1 (c) Cascade of k n-bit adders . Logic for addition of binary vectors.
26
Addition and Subtraction of Signed Numbers
Addition/subtraction logic unit
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.