Escolar Documentos
Profissional Documentos
Cultura Documentos
ADDITION
Addition (decimal)
1 1 11
1 5 5 6 12
4 14 5 5 99
5 19 10 11 111
Addition (binary)
1
0 1 0 1
0 0 1 1
0 1 1 10
Addition (binary)
1 11 1
011 01
01011
11000
Addition (binary)
1
0 1 0 1
0 0 1 1
0 1 1 10
13
11
13
130
143
Multiplication (binary)
1101
1011
1101
11010
1101000
10001111
Multiplication (binary)
10001111
REPRESENTING SIGNED (POSITIVE
AND NEGATIVE) NUMBERS
Representing numbers (ints)
Fixed, finite number of bits.
4. excess bD-1
Signed magnitude
N N
Ex. 4-bit signed magnitude 0 0000 1000
1 bit for sign
1 0001 1001
3 bits for magnitude
2 0010 1010
3 0011 1011
4 0100 1100
5 0101 1101
6 0110 1110
7 0111 1111
Signed magnitude
N N
Ex. 4-bit signed magnitude 0 0000 1000
1 bit for sign
1 0001 1001
3 bits for magnitude
2 0010 1010
3 0011 1011
4 0100 1100
5 0101 1101
6 0110 1110
7 0111 1111
1s complement
(diminished radix complement)
Let x be a non-negative number.
Then x is represented by bD-1+(-x) where
b = base
D = (total) # of bits (including the sign bit)
bD-1 + x
BINARY ARITHMETIC
Signed magnitude
BINARY ARITHMETIC
Addition w/ signed magnitude
algorithm
For A - B, change the sign of B and perform
addition of A + (-B) (as in the next step)
For A + B:
if (Asign==Bsign) then { R = |A| + |B|; Rsign = Asign; }
else if (|A|>|B|) then { R = |A| - |B|; Rsign = Asign; }
else if (|A|==|B|) then { R = 0; Rsign = 0; }
else { R = |B| - |A|; Rsign = Bsign; }
Complicated?
2s complement
BINARY ARITHMETIC
Representing numbers (ints) using
2s complement
Fixed, finite number of bits.
discard carry
0 0111010
11011000
1 00010010
Carry vs. overflow
Addition of 2s complement binary
numbers
Carry vs. overflow when adding A + B
If A and B are of opposite sign, then overflow
cannot occur.
Overflow occurs iff the carry into the sign bit differs
from the carry out of the sign bit.
Addition of 2s complement binary
numbers
#include <stdio.h>
class test {
public static void main ( String args[] ) int main ( int argc, char* argv[] )
{ {
byte A = 127; char A = 127;
byte B = 127; char B = 127;
byte result = (byte)(A + B); char result = (char)(A + B);
System.out.println( "A + B = " printf( "A + B = %d \n", result );
+ result );
} return 0;
} }
Result = -2 in both
Java (left) and C++
(right). Why?
Addition of 2s complement binary
numbers
Result = -2 in both Java and C++.
class test { Why?
public static void main ( String args[] )
Whats 127 as a 2s complement
{
binary number?
byte A = 127;
byte B = 127;
01111111
byte result = (byte)(A + B);
System.out.println( "A + B = "
01111111
+ result );
}
11111110
What is 111111102?
}
Flip the bits: 00000001.
Then add 1: 00000010.
This is -2.
1s complement
BINARY ARITHMETIC
Addition with 1s complement
N N
Note: 1s complement has two
0s! 0 0000 1111
1s complement addition is 1 0001 1110
tricky (end-around-carry). 2 0010 1101
3 0011 1100
4 0100 1011
5 0101 1010
6 0110 1001
7 0111 1000
8-bit 1s complement addition
Ex. Let X = A816 and Y = 8616.
Calculate Y - X using 1s complement.
8-bit 1s complement addition
Ex. Let X = A816 and Y = 8616.
Calculate Y - X using 1s complement.
Y = 1000 01102 = -12110
1000 0110
X = 1010 10002 = -8710
~X = 0101 01112 0101 0111
(Note: C=0 out of msb.)
1101 1101
Y - X = -121 + 87 = -34 (base 10)
8-bit 1s complement addition
Ex. Let X = A816 and Y = 8616.
Calculate X - Y using 1s complement.
8-bit 1s complement addition
Ex. Let X = A816 and Y = 8616.
Calculate X - Y using 1s complement.
X = 1010 10002 = -8710 1010 1000
01111001
Y = 1000 01102 = -12110
~Y = 0111 10012 1 0010 0001
end around
carry 1
(Note: C=1 out of msb.)
0010 0010
X - Y = -87 + 121 = 34 (base 10)
Excess K (biased)
BINARY ARITHMETIC
Binary arithmetic and Excess K
(biased) N N
Method: Simply add and then flip the sign bit. 0 1000 1000
-1 0111
+5 1101
-- ----
1 1001 0111
+4 0100 -> flip sign -> 1100
2 1010 0110
+1 1001
-5 0011 3 1011 0101
-- ----
-4 1100 -> flip sign -> 0100 4 1100 0100
+1 1001
+5 1101
5 1101 0011
-- ----
+6 0110 -> flip sign -> 1110 6 1110 0010
-1 0111 7 1111 0001
-5 0011
-- ---- (Not used for integer arithmetic but employed
-6 1010 -> toggle sign -> 0010 in IEEE 754 floating point standard.)