Binary Numbers and Arithmetic
Binary Numbers and Arithmetic
ADDITION
Addition (decimal)
1 4 5
5 14 19
5 5 10
6 5 11
11
12 99 111
Addition (binary)
0 0 0
1 0 1
0 1 1
1 1 10
Addition (binary)
1 11 1
Addition (binary)
0 0 0
1 0 1
0 1 1
1 1 10
MULTIPLICATION
Multiplication (decimal)
13 11 13 130 143
Multiplication (binary)
1101 1011 1101 11010 1101000 10001111
Multiplication (binary)
Its interesting to note that binary multiplication is a sequence of shifts and adds of the first term (depending on the bits in the second term.
110100 is missing here because the corresponding bit in the second terms is 0.
In general, for k bits, the unsigned range is [0..+2k-1] and the signed range is [-2k-1..+2k-1-1].
Signed magnitude
Ex. 4-bit signed magnitude
1 bit for sign 3 bits for magnitude
0 0000 1000 1 0001 1001 2 0010 1010 3 0011 1011 4 0100 1100 5 0101 1101 6 0110 1110 7 0111 1111
Signed magnitude
Ex. 4-bit signed magnitude
1 bit for sign 3 bits for magnitude
0 0000 1000 1 0001 1001 2 0010 1010 3 0011 1011 4 0100 1100 5 0101 1101 6 0110 1110 7 0111 1111
2 0010 1101 3 0011 1100 4 0100 1011 5 0101 1010 6 0110 1001 7 0111 1000
2 0010 1110 3 0011 1101 4 0100 1100 5 0101 1011 6 0110 1010 7 0111 1001
0 0000 0000 1 0001 1111 2 0010 1110 3 0011 1101 4 0100 1100 5 0101 1011 6 0110 1010 7 0111 1001
0 0000 0000 1 0001 1111 2 0010 1110 3 0011 1101 4 0100 1100 5 0101 1011 6 0110 1010 7 0111 1001
excess bD-1
For pos, neg, and 0, x is represented by
bD-1 + x.
0 1000 1000 1 1001 0111 2 1010 0110 3 1011 0101 4 1100 0100 5 1101 0011 6 1110 0010 7 1111 0001
Ex. Let b=2 and D=4. Then the excess 8 (24-1) representation for 0 is 8+0 = 8 or 10002. Ex. Let b=2 and D=4. Then excess 8 for -1 is 8 1 = 7 or 01112.
signedMag 1sComp 2 sComp excess n 1000 0111 0110 0101 0100 0011 0010 0001
BINARY ARITHMETIC
Signed magnitude
BINARY ARITHMETIC
Complicated?
2s complement
BINARY ARITHMETIC
In general, for k bits, the signed range is [-2k-1..+2k-1-1]. So where does the extra negative value come from?
0 0000 0000 1 0001 1111 2 0010 1110 3 0011 1101 4 0100 1100 5 0101 1011 6 0110 1010 7 0111 1001 8 1000
In general, for k bits, the signed range is [-2k-1..+2k-1-1]. So where does the extra negative value come from?
0 01 01000 01100010
0 0111010
11 1 1
1 1 11
return 0;
}
Result = -2 in both Java and C++. Why? Whats 127 as a 2s complement binary number?
1s complement
BINARY ARITHMETIC
0010 0010
Excess K (biased)
BINARY ARITHMETIC
0 1000 1000
1 1001 0111 2 1010 0110 3 1011 0101 4 1100 0100 5 1101 0011 6 1110 0010 7 1111 0001
(Not used for integer arithmetic but employed in IEEE 754 floating point standard.)