Arithmetic Operations in Binary: Addition / Subtraction 01011 + 111 ? "Method" Exatly The Same As Decimal
Arithmetic Operations in Binary: Addition / Subtraction 01011 + 111 ? "Method" Exatly The Same As Decimal
Addition / subtraction
01011
+ 111
?
X = xn … xi … x0
+ Y = yn … yi … y0
__________________
di
di = (xi + yi ) mod r + carry-in
Addition
For ( i = 0…n ) do
di = (xi + yi + carry-in) mod r
carry-out = (xi + yi + carry-in) div r
End for
Arithmetic operations in binary
Subtraction
Can you write an equivalent method
(algorithm) for subtraction?
di
For ( i = 0…n ) do
di = ( xi yi bi-1 ) mod r
borrow-out = ( xi yi bi-1 ) div r
End for
Subtraction
Arithmetic operations in binary
Multiplication
For ( i = 0…n ) do
di = ( xi yi + ci-1 ) mod r
ci = ( xi yi + ci-1 ) div r
End for
Is this correct?
Negative numbers (4 traditions):
Signed magnitude
Radix complement
Diminished radix complement
Excess-b (biased)
r x ( r 1) x 1
n n
e.g. n = 4, r = 10
7216 --> 9999 - 7216 + 1 = 2784 (10s complement)
n = 4, r = 2
0101 --> 1111 - 0101 + 1 = 1011 (2s complement)
diminished radix complement is
( r 1) x
n
e.g. n = 4, r = 10
7216 --> 9999 - 7216 = 2783 (9s complement)
n = 4, r = 2
0101 --> 1111 - 0101 = 1010 (1s complement)
N = 5 r = 10 N = 32546
[N] = 105 – 32546 = (67454)10
Two’s complement arithmetic
n2
( an-1, an-2, … a0 ) in 2-s complement is a n 1 2 n 1
i
a
i 0
2 i
– Example: (n = 4)
0101 023 + 122 + 02 + 11 = 4 + 1 = 5
1011 123 + 022 + 12 + 11 = 8 + 2 + 1 = 5
– Addition/subtraction (n = 5)
+10 01010
+3 00011
01101 13
Two’s complement arithmetic
– Addition/subtraction (n = 5)
+10 01010
+7 00111 When can overflow
Overflow 10001 15 happen? Only when
both operands have
the same sign and the
+15 01010 sign bit of the result is
13 10011 different.
Discard 100010 2
Cyclic representation (n = 4, r = 2)
avoid discontinuity between
0111 and 1000
Add x:
move x positions clockwise
Subtract x:
move x positions counterclockwise
move (16 - x) positions clockwise
(i.e. add radix complement)
How to detect discontinuity?
-5 1011 overflow
+ -6 + 1010 but, carry into most significant bit is 0 while carry out is 1
-11 10101
Same circuitry
signed numbers
add
subtract (use 2s complement of subtrahend)
Intel architecture OF (overflow flag) detects out-of-range result
unsigned numbers
same protocol
but discontinuity is now between 0000 and 1111
detect with overflow for addition
lack of overflow for subtraction
Intel uses CF (carry flag) to detect out-of-range result
Codes
0, bi bi 1
gi
1, else
e.g.
n = 3: 6 -> 110 -> 101
c c
h - 2c
d = h - 2c -1
2c + d + 1 = 2c + (h - 2c - 1) + 1 = h
Hamming codes: n 2 1 k
e.g. (n = 7)
check information
1 (001) 3 (011), 5 (101), 7 (111)
2 (010) 3 (011), 6 (110), 7 (111)
4 (100) 5 (101), 6 (110), 7 (111)
Code information packets to maintain even parity in groups
e.g. (n = 7)
packet is 1011 => positions 7, 6, 5, 3
7654321
101x1xx
check information
1 3, 5, 7 => bit 1 is 1
2 3, 6, 7 => bit 2 is 0
4 5, 6, 7 => bit 4 is 0
• So true two-bit error corrupts at least one parity group => min distance > 2
• Three-bit error (two msb and lsb) goes undetected => minimum distance = 3