Post on 28-Jun-2018
1
01-24-2018
Integer Encoding and Manipulation
Presentation D
CSE 2421: Systems I
Low-Level Programming and Computer Organization
Gojko Babić
Study: Bryant Chapter 2.1− 2.3
• For 2’s complement, most significant bit indicates sign0 for nonnegative1 for negative
B2T(X) xw12w1 xi 2
i
i0
w2
B2U(X) xi 2i
i0
w1
Binary to Unsignedinterpretation
Binary to Two’s Complementinterpretation
SignBit
Decimal Hex Binary x 15213 3B 6D 00111011 01101101 y -15213 C4 93 11000100 10010011
Unsigned & Signed Integer Encoding
Let binary number X have w-bit pattern: xw-1 xw-2 xw-3 … x3 x2 x1 x0
xi = 0 or 1
Presentation D 2
short int x = 15213;short int y = -15213;
short int x = 0x3B6D;short int y = 0xC493;or
2
x = 15213: 00111011 01101101y = -15213: 11000100 10010011
Weight 15213 ‐15213
1 1 1 1 1 2 0 0 1 2 4 1 4 0 0 8 1 8 0 0
16 0 0 1 16 32 1 32 0 0 64 1 64 0 0
128 0 0 1 128 256 1 256 0 0 512 1 512 0 0
1024 0 0 1 1024 2048 1 2048 0 0 4096 1 4096 0 0 8192 1 8192 0 0
16384 0 0 1 16384 ‐32768 0 0 1 ‐32768
Sum 15213 ‐15213
2’s Complement Encoding Examples
3
g. babic Presentation D 4
Conversion from binary unsigned integer to decimal integer1011012 = 4510 since: 1011012 = 25 + 23 + 22 + 20 = 4510
Conversion from 92710 integer to 16-bit binary integer:927/2 = 463 + 1 LSB 463/2 = 231 + 1 231/2 = 115 + 1 115/2 = 57 +157/2 = 28 + 1 28/2 = 14 + 0 14/2 = 7 + 0 7/2 = 3 + 1 3/2 = 1 + 1 1/2 = 0 + 1
92710 as short int = 00000011100111112
What about negative integers?
Unsigned Binary Decimal
3
Claim: Following holds for 2’s complement integers:~x + 1 = –x
~x is 1’s complement of x change 1’s to 0’s and 0’s to 1’s
Proof:
~x + x = 1111…111 = –1
~x + x = –1 ~x + x +1 –x = –1 +1 –x ~x +1 = –x Example:
x = 1521310 = 00111011011011012
–x = ~x + 1 = 1100010010010010 + 1 = 1100010010010011 Works from positive 2’s complement integer to negative 2’s
complement integer, as well as wise verse. Example: x=0 -x=?
1 0 0 1 0 11 1x
0 1 1 0 1 00 0~x+
1 1 1 1 1 11 1‐1
Negation = Complement + Increment
g. babic Presentation D 5
Unsigned Values
UMin = 0
bit pattern: 000…000
UMax = 2w – 1
bit pattern: 111…111
Two’s Complement Values
TMin = –2w–1
bit pattern: 100…000
TMax = 2w–1 – 1
bit pattern: 011…111
Decimal Hex Binary UMax 65535 FF FF 11111111 11111111 TMax 32767 7F FF 01111111 11111111 TMin -32768 80 00 10000000 00000000 -1 -1 FF FF 11111111 11111111 0 0 00 00 00000000 00000000
Values for W = 16
Numeric Ranges of W-bit Integers
Presentation D 6
4
Observations:
|TMin | = TMax + 1
UMax = 2 * Tmax +1
W
8 16 32 64
UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615
TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807
TMin ‐128 ‐32,768 ‐2,147,483,648 ‐9,223,372,036,854,775,808
Values for Different Word Sizes
Presentation D 7
Integer sizes are platform specific
Our C language platform supports:
• (unsigned) char 1 byte
• (unsigned) short int 2 bytes
• (unsigned) int 4 bytes
• (unsigned) long long int 8 bytes
Equivalence
• Same encodings for nonnegative values
Uniqueness
• Every bit pattern represents unique integer value
• Each representable integer has unique bit encoding
X B2T(X)B2U(X)
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
–88
–79
–610
–511
–412
–313
–214
–115
1000
1001
1010
1011
1100
1101
1110
1111
0
1
2
3
4
5
6
7
4-bit Unsigned & Signed Numeric Values
Presentation D 8
5
Signed
0
1
2
3
4
5
6
7
-8
-7
-6
-5
-4
-3
-2
-1
Unsigned
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Bits
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
=
+/‐ 16
4-bit Signed Unsigned Mapping
9
+ + + + + +• • •
- + + + + +• • •
ux
x
w–1 0
Largest negative weight becomeslarge positive weight
Relation Between Signed & Unsigned
Presentation D 10
• Mappings between unsigned and two’s complement numbers:
keep bit representations,
but reinterpret and can have unexpected effect of adding or subtracting 2w
6
0
TMax
TMin
–1
–2
0
UMax
UMax – 1
TMax
TMax + 1
2’s Complement Range
UnsignedRange
Signed & Unsigned Conversion Visualized
Presentation D 11
• By default, decimal constants are considered to be signed integers.
• Unsigned if have “U” as suffix:0U, 4294967259U
• Explicit casting between signed & unsigned for same size integers doesn’t change bit pattern:
int tx, ty;unsigned int ux, uy;tx = (int) ux;uy = (unsigned) ty;
• Implicit casting also occurs via assignments and function calls:tx = ux;uy = ty;
• Explicit and implicit casting above gives the same results.
Unsigned vs. Signed in C
Presentation D 12
7
• Expression evaluation: If there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsigned;includes arithmetic and relational expressions
• Examples for W = 32, i.e. 32-bit integers • Constant1 Constant2 Relation Evaluation
0 0U == unsigned-1 0 < signed-1 0U > unsigned
2147483647 -2147483647-1 > signed2147483647U -2147483647-1 < unsigned
-1 -2 > signed(unsigned) -1 -2 > unsigned2147483647 2147483648U < unsigned 2147483647 (int) 2147483648U > signed
• TMin=-2,147,483,648, TMax=2,147,483,647, UMax=4,294,967,295
Casting Surprises
Presentation D 13
• Expanding refers to converting shorter-bit integer into longer-bit integer, e.g. short int into int
Unsigned integers: zeros added
Signed integers: sign extension Interesting case: when a shorter signed variable is assigned
to a longer unsigned variable E.g.: unsigned int x; short int y;
x = y; y is first sign extended to 32-bits, then that value is assigned to x
• Truncating refers to converting longer-bit integer into shorter-bit integer, e.g., unsigned into unsigned short
Unsigned & signed integers: bits are truncated and results are reinterpreted
For unsigned integers could be thought as mod operation
Expanding & Truncating Integers
Presentation D 14
8
• Task: Given w-bit signed integer X convert it to w+k-bitinteger X’ with same value.
• Rule: Make k copies of sign bit:
X’ = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0
k copies of XW–1
• • •X
X • • • • • •
• • •
w
wk
Presentation D 15
Sign Extension
• C automatically performs sign extension when converting from smaller to larger integer data type
short int x = 15213;int ix = (int) x; //explicit casting not neededshort int y = -15213;int iy = (int) y; //explicit casting not needed
Decimal Hex Binary
x 15213 3B 6D 00111011 01101101ix 15213 00 00 3B 6D 00000000 00000000 00111011 01101101y -15213 C4 93 11000100 10010011iy -15213 FF FF C4 93 11111111 11111111 11000100 10010011
g. babic Presentation D 16
Sign Extension Examples
9
int main() /*Expanding.c*/
{
short int si1=0x85FF;
unsigned short su1=0x85FF;
int i1;
unsigned int u1;
i1=su1;
u1=si1;
printf("su1= %hX i1= %.8X", su1, i1);
printf("\nsi1= %hX u1= %.8X", si1, u1);
}g. babic Presentation D 17
Mixing Integer Types: Example 1
%X an integer argument is
printed in hex;
%hX a short int argument is
printed in hex;
Output:
su1= 85FF i1= 000085FF
si1= 85FF u1= FFFF85FF
int main() {
int i1 = 70000, i2;
short si1 = -70, si2, si3;
si2=i1/si1;
si3= (short)i1/si1;
i2= (short)(i1/si1);
printf("i1%%d= %d i1%%hd= %hd si1= %hd \nsi2= %hd si3= %hd i2 =%d", i1, i1, si1, si2, si3, i2);
}Output:
i1%d= 70000 i1%hd= 4464 si1= -70
si2= -1000 si3= -63 i2= -1000
g. babic Presentation D 18
Mixing Integer Types: Example 2
10
• In the previous example little endian byte ordering assumed.
• Big Endian
Least significant byte has highest address
• Little Endian
Least significant byte has lowest address
• Example: Variable X has 4-byte content 0x01234567 at address 0x1000; thus X includes consecutive bytes at addresses 0x1000, 0x1001, 0x1002 and 0x1003
Big Endian
Little Endian
0x1003 0x1002 0x1001 0x1000
01 23 45 6767 45 23 01
0x1003 0x1002 0x1001 0x1000
67 45 23 0101 23 45 67
Byte Ordering
Presentation D 19
• Bitwise operators perform the logical operations and (&),
or (|), xor (^), and 1’s complement (~).
• Those operations may be applied to any integer data type: signed/unsigned, long, int, short, char and they view arguments as bit vectors
• Function applied bit-wise
• All of the properties of Boolean algebra apply
Bitwise Operations in C
Presentation D
01101001& 01010101001
01101001|01010101
01101001^ 0101010100111100
~ 0101010110101010
01000001 01111101 00111100 10101010
20
11
• Logical operators and (&&), or (II) and not (!) view 0 as “false”, anything nonzero as “true”
Always return 0 or 1 with early termination
Examples
!0x41 ➙ 0x00
!0x00 ➙ 0x01
0x69 && 0x55 ➙ 0x01
0x69 || 0x55 ➙ 0x01
• Examples with bit level operators &, |, ~; no ^ counterpart
~0x41 ➙ 0xBE ~01000001 ➙ 10111110
~0x00 ➙ 0xFF ~00000000 ➙ 11111111
0x69 & 0x55 ➙ 0x41 01101001 & 01010101 ➙ 01000001
0x69 | 0x55 ➙ 0x7D 01101001 | 01010101 ➙ 01111101Presentation D
Contrast with Logical Operators in C
21
• Shift operations may be applied to any integer data type
• Left shift: x << y
• Shift bit-vector x left y positions
Throw away extra bits on left
Fill with 0’s on right
• Right shift: x >> y
• Shift bit-vector x right y positions
Throw away extra bits on right
• Logical shift
Fill with 0’s on left
• Arithmetic shift
Replicate most significant bit on left
• Undefined behavior if shift amount < 0 or ≥ word size
01100010Argument X
00010000<< 3
00011000Logical >>2
00011000Arithmetic>>2
0001000000010000
0001100000011000
0001100000011000
10100010Argument X
00010000<< 3
00101000Logical >>2
11101000Arithmetic>>2
00010000
00101000
11101000
00010000
00101000
11101000
Shift Operations in C
Presentation D 22
12
• C performs right shifts on signed integers as arithmetic shifts
• C performs right shifts on unsigned integers as logical shifts
int main() { /*Program Shift.Short.c */
int i1=0x81234567, i2, i3;
unsigned u1=0x81234567, u2, u3;
i2 = i1 >> 8;
u2 = u1 >> 8;
i3 = i1 << 8;
u3 = u1 << 8;
printf("i1= 0x%x i2= 0x%.8X i3= 0x%x", i1, i2, i3);
printf("\nu1= 0x%x u2= 0x%.8x u3= 0x%x", u1, u2, u3);
}
Output:
i1= 0x81234567 i2= 0xFF812345 i3= 0x23456700
u1= 0x81234567 u2= 0x00812345 u3= 0x23456700
Shifts on Signed & Unsigned Integers
Presentation D 23g. babic
24
• In addition to arithmetic compounded assignments, such as
+= and %=, C provides more derivatives by combining
computation and assignment together.
&= bitwise AND assignment
|= bitwise OR assignment
^= bitwise exclusive OR assignment
<<= left shift assignment
>>= right shift assignment
• Examples:
i &= 3 i = i & 3
i <<= 3 i = i << 3
Compounded Assignments
Presentation D
13
g. babic Presentation D 25
Adding Binary Numbers
ai bi Carry Ini Sumi CarryOuti
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Input Output
1-bit Adder Truth Table
2. a = 11010111b = 11010001
a + b = 110101000
3. a = 01010011b = 01010001
a + b = 010100100
1. a = 00010011b = 01010001
a + b = 001100100
4. a = 01110011b = 11010001
a + b = 101000100
Let us analyze each case assumingaddition of unsigned or signed integers
5. a = 10110011b = 10010001
a + b = 101000100
g. babic 26
Analysis of Examples on Previous Slide
2. a = 11010111 As signed a=-41, b=-47, a + b = -88 correct
b = 11010001 As unsigned a=215, b=209, a + b = 168 incorrect
a + b = 110101000 Final carryout == 1 unsigned overflow
3. a = 01010011 As signed a=+83, b=+81, a+b = -92 incorrect
b = 01010001 As unsigned a=83, b=81, a+b = 164 correct
a + b = 010100100 Adding 2 positive gives negative signed overflow
1. a = 00010011 As signed a=+19, b=+81, a + b = +100 correct
b = 01010001 As unsigned a=19, b=81, a + b = 100 correct
a + b = 001100100
4. a = 01110011 As signed a=+115, b=-47, a+b=+68 correct
b = 11010001 As unsigned a=115, b=209, a+b=68 incorrect
a + b = 101000100 Final carry out == 1 unsigned overflow5. a = 10110011 As signed a=-77, b=-111, a+b = +68 incorrect
b = 10010001 As unsigned a=179, b=145, a+b=68 incorrect
a + b = 101000100 Adding 2 negative gives positive signed overflowFinal carry out == 1 unsigned overflow
14
• If operands have w bits, true sum has w+1 bits
• But standard C addition function ignores (w+1)st bit (final carry output), i.e. (w+1)st bit is truncated.
• If final carry out is 1, we are left with wrong result overflow
• UAddw(u , v) = (u + v) mod 2w
UAdd w (u,v ) u v u v 2 w
u v 2w u v 2 w
UAddw: Unsigned Addition
Presentation D 27
Overflow case
• • •
• • •
u
v+
• • •u + v
• • •UAddw(u , v)
TAddw: Two’s Complement Addition
True Sum: w+1 bits
Operands: w bits
Discard carryout: w bits TAddw(u , v)
• • •
• • •
u
v+
• • •u + v
• • •
28
• Interesting property: 2’s complement addition function is same if operands have same or different signs.
• Compare with decimal addition!Presentation D
TAddw (u,v) u v 2w1 u v TMinw
u v TMinw u v TMaxw
u v 2w1 TMaxw u v
Negative overflow
Positive overflow
15
• TAddw and UAddw have identical bit-level behavior
• Signed vs. unsigned addition in C:
int s, t, u, v;
s = (int) ((unsigned) u + (unsigned) v);
t = u + v /* s == t */
• How to detect unsigned addition overflow?
If the final carryout is 1. Why?
If the sum is smaller than any of operands. Why?
• How to detect 2’s complement addition overflow?
If adding 2 negative operands results in positive sum or if adding 2 positive operands results in negative sum. Why?
If carryin in the most significant bit is different from final carryout. Why?
Binary Addition: Summary
g. babic Presentation D 29
30
A B A and B
0 0 0
0 1 0
1 0 0
1 1 1
A B A or B
0 0 0
0 1 1
1 0 1
1 1 1
ANDLogical
operationXOROR
Comp-lement
01
0
~AA
1
A B A xor B
0 0 0
0 1 1
1 0 1
1 1 0
Logical Operations and Gates
• Gates are simplest digital logic circuits, and they implement basic logical operations (functions).
• Gates are designed using few resistors and transistors.• Gates are used to build more complex circuits that implementmore complex logic functions.
Gates
g. babic Presentation D
16
g. babic Presentation D 31
1-bit Adder
Sum
CarryIn
CarryOut
a
b
ai bi CarryIni Sumi CarryOuti
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
• 32-bit adder can be built out of 32 1-bit adders
Input Output
1-bit Adder Truth Table
ith 1-bit Adder
Sumi
bi
ai
CarryIni = Carryouti-1
Carryouti
Designing 1-bit Adder
• A part of 1-bit adder design that calculates carryout
b
C a r r y O u t
a
C a r r y I n
g. babic Presentation D 32
17
#include<stdio.h>
void main()
{
char a=83, b=81, c;
printf ("a=%hd, a=0x%hx",a, a);
printf (" b=%hd, b=0x%hx",b, b);
c=a+b;
printf ("\n c=%d, c=0x%x",c, c);
printf ("\n a+b=%d, a+b=0x%.8x",a+b, a+b);
}
g. babic Presentation D 33
fl1|~/Cse2421> add2chara=83, a=0x53 b=81, b=0x51c=-92, c=0xffffffa4a+b=164, a+b=0x000000a4
2’s Complement Overflow
g. babic Presentation D 34
32-bit Ripple Carry Adder
+
+
+
+
a0
b0
a2
b2
a1
b1
a31
b31
sum0
sum31
sum2
sum1
Cout
Cin
Cout
Cout
Cout
Cin
Cin
Cin
“0”
Carry out
18
g. babic Presentation D 35
32-bit Subtractor“0”
a31
b31
+
+
+
+
a0
b0
a2
b2
a1
b1
Result0
Result31
Result2
Result1
Cout
Cin
Cout
Cout
Cout
Cin
Cin
Cin
CarryOut
“1”
A – B = A + (–B)
= A + ~B + 1
• x < 0 implies x*2 < 0 ?
No if overflow into positive!
• ux >= 0 ?
Yes.
• x&7 == 7 implies (x << 30) < 0 ?
Yes.
• ux > -1 ?
Never. Umax == -1!
• x > y implies -x < -y ?
No if y == Tmin!
• x*x >= 0 ?
No if overflow into negative!
Which of Statements Below Are Always True?Initialization: int x = foo();
int y = bar();unsigned ux=x;unsigned uy=y;
• x > 0 && y > 0 implies x + y > 0 ?No if overflow into negative!
• x >= 0 implies -x <= 0 ?Yes.
• x <= 0 implies -x >= 0 ?No if x == Tmin.
• (x | -x) >> 31 == -1 ?No if x == 0
• ux >> 3 == ux / 8 ?Yes.
• x >> 3 == x / 8 ?No if x negative!int x = -19;y = x >> 3; // y = -3int z = x / 8; // z = -2
• x & (x-1) != 0 ?No. x == 0 or x == 1! or x==Tmin
36Presentation D
19
• Computing exact product of w-bit unsigned numbers x, y
• Range of results: 0 ≤ x * y ≤ (2w – 1) 2 = 22w – 2w+1 + 1
up to 2w bits needed
• But standard C unsigned multiplication function ignores high order w bits and gives as a result of multiplication lower w bits.
• C implements modular arithmetic: UMultw(u , v) = u * v mod 2w
• • •
• • •
u
v*
• • •u * v
• • •
True product: 2*w bits
Operands: w bits
Discard w bits: w bits UMultw(u , v)
• • •
Unsigned Multiplication in C
Presentation D 37
• Computing exact product of w-bit 2’s complement numbers x, y
• Range of results:
min: x *y ≥ (–2w–1)*(2w–1–1) = –22w–2 + 2w–1 up to 2w –2 bits
max: x *y ≤ (–2w–1) 2 = 22w–2 up to 2w – 1 bits
• As for unsigned, standard C signed multiplication function ignores high order w bits.
Signed Multiplication in C
• • •
• • •
u
v*
• • •u * v
• • •
True product: 2*w bits
Operands: w bits
Discard w bits: w bits TMultw(u , v)
• • •
Presentation D 38
• Although some of ignored bits are different for signed vs. unsigned multiplication, lower w bits are the same.
• But, how do we figure out if lower w bits are the complete product or we have lost some significant bit values in higher w bits?
20
Mode x y x*y Truncated x*y
unsigned 510 = 1012 310 = 0112 1510 = 0011112 1112 = 710
2’s comp -310 = 1012 310 = 0112 -910 = 1101112 1112 = -110
unsigned 410 = 1002 710 = 1112 2810 = 0111002 1002 = 410
2’s comp -410 = 1002 -110 = 1112 410 = 0001002 1002 = -410
unsigned 310 = 0112 310 = 0112 910 = 0010012 0012 = 110
2’s comp 310 = 0112 310 = 0112 910 = 0010012 0012 = 110
g. babic Presentation D 39
Multiplication Examples in C• Below shown are three examples of multiplication of two 3-bit
unsigned and three examples of two 2’s complement numbers with identical bit patterns.
• Although the bit-level representations of the full products may differ, those of the truncated product are identical for signed and unsigned multiplication.
int main() {int i2; long i3,i4; short si1 = -1000;i2= si1*si1*si1*si1*si1;i3= si1*si1*si1*si1*si1;i4=si1;i4= i4*si1*si1*si1*si1;printf("i2%%d= %d", i2);printf("\ni2%%hd= %hd", i2);printf("\ni2%%ld= %ld", i2);printf("\ni3%%d= %d", i3);printf("\ni3%%hd= %hd", i3);printf("\ni3%%ld= %ld", i3);printf("\ni4%%d= %d", i4);printf("\ni4%%hd= %hd", i4);printf("\ni4%%ld= %ld", i4);}
Mixing Integer Types: Example 3
40
fl1|~/Cse2421> MixInt1i2%d= 1530494976i2%hd= -32768i2%ld= 1530494976i3%d= 1530494976i3%hd= -32768i3%ld= 1530494976i4%d= 1530494976i4%hd= -32768i4%ld= -1000000000000000
21
• Operation u << k (left shift) is equivalent to u *2k for both signed and unsigned.
• Most machines shift and add much faster than multiply and if one of operands is constant, compilers generate code where an multiplication is replaced by shifts and adds.
• Examples:
x = u * 8 could be compiled as:
x = u << 3
x = u * 24 could be compiled as:
x = (u << 5) − (u<<3) //There is a better way?
x= u * 12 could be compiled as:
x = (u<<1 + u) << 2
Power-of-2 Multiply Using Shifts
g. babic Presentation D 41
• Divide is also much slow than shift. Quotient of an unsigned integer by power of 2 is equivalent to logical right shift, i.e. u >> k gives u / 2k.
Division Computed Hex Binary x 15213 15213 3B 6D 00111011 01101101 x >> 1 7606.5 7606 1D B6 00011101 10110110 x >> 4 950.8125 950 03 B6 00000011 10110110 x >> 8 59.4257813 59 00 3B 00000000 00111011
Power-of-2 Divide Using Shifts
Division Computed Hex Binary y -15213 -15213 C4 93 11000100 10010011 y >> 1 -7606.5 -7607 E2 49 11100010 01001001 y >> 4 -950.8125 -951 FC 49 11111100 01001001 y >> 8 -59.4257813 -60 FF C4 11111111 11000100
42
• Similarly, a quotient of signed integer by power of 2 would be equivalent to arithmetic right shift, i.e. u >> k would give u / 2k.
• Arithmetic shift works correct for positive numbers (as logical shift does for unsigned), but for negative integers a result rounds in wrong direction.
• What is a correction for negative integers?Presentation Dg. babic
22
g. babic Presentation D 43
• Multiplication is more complicated than addition and it can beaccomplished by shifting and additions.
• But more time and more area required• Example of unsigned multiplication mimicking decimal multiply:
5-bit multiplicand 110012 = 2510
5-bit multiplier ×100112 = 1910
1100111001
0000000000
11001 . 1110110112 = 47510
• But, this algorithm is very impractical to implement because of a problem to perform addition of many operands at once.
Multiplication
g. babic Presentation D 44
• The multiplication can be done with intermediate additions.
• We introduce Product with a double number of bits initialized by
multiplier in lower half and 0’s in upper half.• The same example: multiplicand 11001
Intermediate Product initially 00000 10011
Add multiplicand since Product bit 0 = 1 011001 10011
Shift Product right; Step 1 done 01100 11001
Add since Product bit 0 = 1 100101 11001
Shift Product right; Step 2 done 10010 11100
Shift Product right, no addition bit=0; Step 3 done 01001 01110
Shift Product right, no addition bit=0; Step 4 done 00100 10111
Add since Product bit 0 = 1 011101 10111
Shift Product right, Step 5 done 01110 11011
Finale result in Product = 01110 110112 = 47510
Unsigned Integers Multiplication
23
g. babic 45
Unsigned Integer MultiplicationMultiplicand & Multiplier are w-bit unsigned integers. Initially, 2w-bit Product has Multiplier in its w lower bits and zeros in its w higher bits.
TestProduct bit 0
Shift Product 1-bit logical right
w repetitions?
Yes
Result in Product Register
== 0
No
Add Multiplicand to the left half of Product and store
the result to the left half of Product
Start
== 1
Shift Product 1-bit right; Shift in the
final carry out from the last addition
Presentation D
g. babic Presentation F 46
• A “simple” algorithm that uses unsigned integer multiplication: If needed, convert to positive integer any negative
operand and remember original signs Perform multiplication of unsigned integers Negate product if original signs disagree
• This algorithm is not simple to implement since: it has to account in advance about signs, if needed, convert operands from negative to positive
integers, if needed, convert result back to negative integer at the
end• Booth’s algorithm for 2’s complement multiplication doesn’t
require converting operands between negative and positive integers.
Multiplication of 2’s Complement Integers
24
g. babic 47
Booth’s Algorithm
TestProduct bit 0 and last time shifted out
bit
Shift Product1-bit arithmetic right
w repetitions?
Yes
Result in Product Register
== 00 or 11
No
Add Multiplicand to the left half of Product and store
the result to the left half of Product
Subtract Multiplicand from the left half of Product and store the result to the left half of
Product
Start
== 10
== 01
Multiplicand & Multiplier are w-bit signed integers. Initially, 2w-bit Product has the Multiplier in its w lower bits and zeros in its w higher bits.
g. babic Presentation D 48
6-bit (signed) multiplicand 110101 = -1110 ; Note -110101 = 0010116-bit (signed) multiplier 011101 = +2910
Product register 000000 011101 0 assumed for step1.001011 10 – subtract (i.e. add 001011)001011 011101
shift 000101 101110| 1 step 1. ends110101 01 - add111010 101110
shift 111101 010111| 0 step 2. ends001011 10 - subtract001000 010111
shift 000100 001011| 1 step 3. ends11 - no arithmetic
shift 000010 000101| 1 step 4. ends11 - no arithmetic
shift 000001 000010| 1 step 5. ends
Booth’s Algorithm: Example A
25
g. babic Presentation D 49
shift 000001 000010| 1 step 5. ends (repeated line)
110101 01 - add
110110 000010|
shift 111011 000001| 0 step 6. ends
Product = 111011 0000012 = - 000100 1111112
= - (256+63) = - 31910
• Note, that Booth’s algorithm has nice property that we do not have to worry in advance about signs of operands.
• It can be shown (by example, see Example B next)) that the algorithm produces a product with incorrect sign if the multiplicand is Tmin = -231; thus an update to the algorithm should be made for that case.
Booth’s Algorithm: Example A (continued)
50
6-bit (signed) multiplicand 100000 = -3210 ; Note -100000 = 10000006-bit (signed) multiplier 111101 = -310
Product register 000000 111101 0 assumed for step1.100000 10 – subtract (i.e. add 100000)100000 111101
shift 110000 011110| 1 step 1. ends100000 01 - add010000 011110
shift 001000 001111| 0 step 2. ends100000 10 - subtract101000 001111
shift 110100 000111| 1 step 3. ends11 - no arithmetic
shift 111010 000011| 1 step 4. ends11 - no arithmetic
shift 111101 000001| 1 step 5. ends11 - no arithmetic
shift 111110 100000| 1 step 6. ends
Product = 1111101000002 = -9610 instead of +9610
Booth’s Algorithm: Example B