Chapter 4

advertisement
Binary Arithmetic Operations
Chapter 4
Objective: Algorithm design and analysis for binary arithmetic operations.
Covers: Shift, Addition, Subtraction, Multiplication, Division operations for integers
and floating point numbers.
Shifting Binary Numbers
Representation
Unsigned
Signed
SignedMagnitude
Left Shift
Shift out
Positive Number
Shift out b30 ;
Insert a 0 into
One’s
Complement
Shift out
Shift out
b0
Insert a 0 into
Increase biasedexponent by 1
b0
b30 ;
Insert a 1 into
Shift out
b0
Shift out
Negative Number
Shift out b30 ;
Shift out
b30 ;
Insert a 0 into
Floating Point
b0
b30 ;
Insert a 0 into
Two’s
Complement
Right Shift
b31 , shift a 0 into b0
b30 ;
Insert a 0 into
Positive Number
Shift out b0 ;
Insert a 0 into
Shift out
b0
Copy
Increase biasedexponent by 1
Copy
b30
b0 ;
b31 into b30
Shift out
b0
b0 , shift a 0 into b31
b0 ;
b31 into b30
Decrease biasedexponent by 1
Negative Number
Shift out b0 ;
Insert a 0 into
Shift out
Copy
b0 ;
b31 into b30
Shift out
Copy
b0 ;
b31 into b30
Decrease biasedexponent by 1
Notes:
1. For unsigned integers, we do logic shift when shifting right;
2. For one’s and two’s complement integers, we do arithmetic shift when shifting
right.
3. For signed-magnitude integers, we retain the sign bit and do logic shift for the
magnitude.
4. When shifting left signed integers, the sign bit remains unchanged.
Logic shift: Shift out b0 , shift a 0 into b31
Arithmetic shift: Copy b31 into b30
Addition and Subtraction
Let A  a31a30  a1a0 and B  b31b30 b1b0 be the two operands. Let Z  A  B .
Denote the sign and the magnitude of X by sign X and mag X , respectively. Let c i be
the carry into the i th position.
Unsigned integers:
1. Addition: Do addition, discard c32 if exists.
2. How about subtraction? – 2’s complement subtraction
Signed-Magnitude numbers:
b30
1. Addition:
sign A  sign B
sign A  sign B
sign Z
sign A
magZ
Overflow?
When c31  1
mag A  magB
No overflow
Do y  mag A  magB . If
negative, then sign Z  sign B and
magZ is the 2’s complement of
y . Otherwise, sign Z  sign A
and magZ  y .
2. Subtraction: change the sign of the subtrahend and then proceed as in addition.
3. Performance: In 25% of time, needs two operations. Needs an adder and a
subtracter.
One’s-complement numbers:
1. Addition: Add the two numbers including the sign bits; add the end-around carry
if exists.
2. Subtraction: Take one’s-complement of the subtrahend and then proceed as in
addition.
3. How to detect overflow?
4. Performance: In 50% of time, needs two operations.
Two’s-complement numbers:
5. Addition: Add the two numbers including the sign bits; discard c32 .
6. Subtraction: Take two’s-complement of the subtrahend and then proceed as in
addition.
7. How to detect overflow?
8. Performance: Only one operation.
Multiplication
Unsigned Integers:
1. Add & Shift method: logic shift
2. Performance: 1.5n vs. 2n
3. Example: 010001 001011
Signed-Magnitude Numbers: Z  X  Y
1. sign Z  sign X  signY
2. Do unsigned multiplication for the magnitude, replace the sign bit by a 0.
3. Performance: 1.5n vs. 2n
4. Example: 010001 101011
One’s Complement Numbers:
1. Algorithm is complex but easy to be understood.
2. Add & Shift method: Arithmetic shift and One’s-complement addition; When
multiplier is negative, needs to compute its one’s-complement.
3. Performance: 1.5n+1 vs. 2n+1
4. Example: 2  3 , that is 0010  1100 and  2  3 , that is 1101 0011 .
Two’s Complement Numbers: Booth’s Algorithm
1. Do arithmetic shift and Two’s-complement addition/subtraction
2. Inspecting two bits and shift one bit
Case
00
01
10
11
Operations
Shift
Add + Shift
Sub + Shift
Shift
3. Rationale
4. Inspecting three bits and shift two bits
Case
Bits inspected Operations
000
001
010
011
100
101
110
111
5. Example: 10011101 10110011
6. Performance: 0.875n vs. n
Reason
Division
1. We will only study the signed-magnitude division!
2. Division operation is just the opposite of the multiplication operation
3. Dividend, divisor, quotient, remainder, and their relationships.
 Dividend = divisor * quotient + remainder
 The sign of the quotient will be negative if the dividend and divisor has
different sign
 The sign of the remainder is the same as that of the dividend.
Restoring Method:
1. Description
2. Example: Divide 01110011 by 00000110.
3. It is a shift-subtract procedure
4. How many iterations?
5. Performance: 2.5n vs. 3n
Nonrestoring Method:
1. Description: IN the first step, do shift-sub. In other steps, do either shift-sub or
shift add.
2. Example: Divide 01110011 by 00000110.
3. Performance: 2n vs. 2n
Download