Lec05 - Computer Arithmetic - Integer Representation and Arithmetic
Lec05 - Computer Arithmetic - Integer Representation and Arithmetic
Lec05
Computer Arithmetic – I
Integers Representation and Arithmetic (Addition and Subtraction)
Contents
3
Arithmetic and logic unit (2)
Handles integers
4
ALU inputs and outputs
Data are presented to ALU in registers and the results of an operation are
stored in registers.
Connected by signal paths to the ALU
ALU may set flags as the result of an operation
Control unit provides signals
for controlling the operation of ALU
Movement of the data into and out of the ALU
5
Integer Representation
n-1
A = Σ 2i ai
i=0
6
Sign-Magnitude Representation (1)
Left most bit is sign bit
0 means positive
1 means negative
Examples:
+24 = 00011000
-24 = 10011000
n-2
A = Σ 2i ai, if an-1 = 0
i=0
A = - Σ 2i ai, if an-1 = 1
i=0
7
Sign Magnitude Representation (2)
- Drawbacks
Sign Magnitude Rep. is rarely used for implementing the integer portion of
ALU
• While performing addition/subtraction of numbers in sign-magnitude representation,
– First Compare the sign of two operands
– If the sign are different, then compare the magnitudes of the operands
– Then perform the subtraction of smaller magnitude number from the larger
magnitude number
– Put the sign of larger magnitude number as the resultant sign.
– Requirement of additional hardware circuitry needed to compare the sign as well as
magnitude.
– Time taken for performing addition/subtraction will also be more.
• Two representations of zero (+0 and -0)
– difficult to test for zero
– always should convert from -0 to +0 .
8
Ones' complement
9
Ones' complement – characteristics
10
Twos Complement Representation
- Characteristics (1)
12
Twos Complement Representation
- Characteristics (3)
Expansion of Bit length: Add additional bit positions to the left and fill in
Overflow Rule: If two numbers with the same sign are added, then
overflow occurs if and only if the result has the opposite sign.
and add it to A.
13
Twos Complement Representation
- Examples
0 = 00000000
-1 = 11111111
-2 = 11111110
-3 = 11111101
-45 = 11010011
14
Expression for Twos complement representation
Range of positive integers represented = 0 (all the magnitude bits are 0’s)
through (2n-1 ) -1 ( all the magnitude bits are 1’s)
Example: For 8 bit, 00000000 (0) through 01111111 (127 (i.e) ( 27-1))
n-2
A = - 2n-1 an-1 + Σ 2i ai
i=0
15
Advantages for using Twos complement
Representation
16
Twos Complement Representation
- Negation Special Case 1
0= 00000000
Add 1 to LSB +1
Result 1 00000000
-0=0
17
Twos Complement Representation
-Negation Special Case 2
-128 = 10000000
bitwise NOT 01111111
Add 1 to LSB +1
Result 10000000
So:
-(-128) = -128 X
Monitor MSB (sign bit)
It should change during negation
There is no +128 for 8-bit twos complement representation.
18
Twos Complement Representation
- Negation special case 2 -discussion
19
Use of a Value Box for Conversion between
Twos Complement Binary and Decimal (1)
-128 64 32 16 8 4 2 1
-128 64 32 16 8 4 2 1
1 0 0 0 0 0 1 1
-128 +2 +1
= -128 +2 +1 = -125
20
Use of a Value Box for Conversion between
twos Complement Binary and Decimal (2)
representation
-120 = -128 + 8
-128 64 32 16 8 4 2 1
-128 +8
1 0 0 0 1 0 0 0
-120 = 10001000
21
Conversion between different bit lengths (1)
23
Biased Representation
25
Different Representations for 4-bit Integers
Decimal Representation Sign-Magnitude Twos Complement Biased Representation
Representation Representation
+8 — — 1111
+7 0111 0111 1110
+6 0110 0110 1101
+5 0101 0101 1100
+4 0100 0100 1011
+3 0011 0011 1010
+2 0010 0010 1001
+1 0001 0001 1000
+0 0000 0000 0111
–0 1000 — —
–1 1001 1111 0110
–2 1010 1110 0101
–3 1011 1101 0100
–4 1100 1100 0011
–5 1101 1011 0010
–6 1110 1010 0001
–7 1111 1001 0000
–8 — 1000 —
26
Fixed point representation
representation
Called so since the binary point is fixed and assumed to be the right of the right-most
digit
can use the same representation for fractions by scaling the numbers
27
Integer Arithmetic
- Addition and Subtraction
i.e. a - b = a + (-b)
28
Addition of Numbers in Twos Complement
Representation
(-7) + (+5) (-4) + (+4)
1001 1100
+0101 +0100
1110 = -2 1 0000 = 0
(+3) + (+4) (-4) + (-1)
0011 1100
+0100 +1111
0111 = 7 1 1011 = -5
(+5) + (+4) (-7) + (-6)
0101 1001
+0100 +1010
1001 = overflow 1 0011 =overflow
29
Condition for overflow
If two numbers are added, and they are both positive or both negative,
then overflow occurs if and only if the results has the opposite sign.
30
Detection of overflow
Carry out of sign bit and carry into the sign bit will be given as two inputs
to overflow flip-flop.
If carry out of sign bit and carry into sign bit are equal - NO overflow
If carry out of sign bit and carry into sign bit are not equal - OVERFLOW
occurs
31
Subtraction of Numbers in Twos Complement
Representation
To subtract one number (subtrahend) from another (minuend), take the twos
32
Subtraction of Numbers in 2’s Complement
Representation (M-S = M+(-S))
0010 0101
+1001 +1110
1011 = -5 1 0011 = 3
M = 2 = 0010 M = 5 = 0101
S = 7 = 0111 S = 2 = 0010
-S = 1001 -S = 1110
1011 0101
+1110 +0010
1 1001 = -7 0111 = 7
M = -5 = 1011 M = 5 = 0101
S = 2 = 0010 S = -2 = 1110
-S = 1110 -S = 0010
0111 1010
+0111 +1100
1110 = overflow 1 0110 = overflow
M = 7 = 0111 M = -6 = 1010
S = -7 = 1001 S = 4 = 0100
-S = 0111 -S = 1100 33
Geometric Depiction of Twos
Complement Integers (1)…
34
Geometric Depiction of Twos
Complement Integers (2)
35
Hardware for Addition and Subtraction (1)
36
Hardware for Addition and Subtraction (2)
For binary addition, the two numbers are presented to the adder from
two registers, designated as A and B.
The result may be stored in one of these registers or in a third register.
The overflow indication is stored in a 1-bit overflow flag
0 indicates no overflow while 1 indicates overflow
37
Problem (1)
arithmetic:
a. 111000 - 110011
b. 11001100 - 101110
c. 111100001111 - 110011110011
d. 11000011 - 11101000
38
Problem (2)
a. (85)+(65)
b. (-75)+(67)
c. (127) – (45)
d. (-128) – (-100)
39
Problem (3)
What is the range of numbers that can be represented in 8 bit and 16 bit
40
Problem(4)
Consider the following operation on a binary word. Start with the least significant
bit, copy all bits that are 0 until the first 1 bit is reached and copy that bit , too.
Then take the complement of each bit thereafter. What is the result?
41
Reference
42