Você está na página 1de 50

Binary numbers and arithmetic

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

So can we count in binary?


Counting in binary (4 bits)
0 0000
1 0001
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MULTIPLICATION
Multiplication (decimal)

13
11
13
130
143
Multiplication (binary)

1101
1011
1101
11010
1101000
10001111
Multiplication (binary)

Its interesting to note


1101
1011
that binary multiplication
is a sequence of shifts
and adds of the first
term (depending on the 1101
bits in the second term.
11010 110100 is missing here
because the
1101000 corresponding bit in the
second terms is 0.

10001111
REPRESENTING SIGNED (POSITIVE
AND NEGATIVE) NUMBERS
Representing numbers (ints)
Fixed, finite number of bits.

bits bytes C/C++ Intel Sun


8 1 char [s]byte byte
16 2 short [s]word half
32 4 int or long [s]dword word
64 8 long long [s]qword xword
Representing numbers (ints)
Fixed, finite number of bits.

bits Intel signed unsigned


8 [s]byte -27..+27-1 0..+28-1
16 [s]word -215..+215-1 0..+216-1
32 [s]dword -231..+231-1 0..+232-1
64 [s]qword -263..+263-1 0..+264-1

In general, for k bits, the unsigned range is [0..+2k-1] and the


signed range is [-2k-1..+2k-1-1].
Methods for representing signed
ints.
1. signed magnitude

2. 1s complement (diminished radix


complement)

3. 2s complement (radix complement)

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)

Ex. Let b=2 and D=4.


Then -1 is represented by 24-1-1 = 1410 or 11102.
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)
Ex. What is the 9s complement of 1238910?
Given b=10 and D=5. Then the 9s complement of 12389
= 105 1 12389
= 100000 1 12389
= 99999 12389
= 87610
1s complement
(diminished radix complement)
Let x be a non-negative number. N N
Then x is represented by bD- 0 0000 1111
1+(-x) where 1 0001 1110
b = base 2 0010 1101
D = (total) # of bits (including the 3 0011 1100
sign bit)
4 0100 1011
Shortcut for base 2?
5 0101 1010
All combinations used, but 2
zeros! 6 0110 1001
7 0111 1000
2s complement
(radix complement)
Let x be a non-negative number.
Then x is represented by bD+(-x).
Ex. Let b=2 and D=4. Then -1 is represented by 24-
1 = 15 or 11112.
Ex. Let b=2 and D=4. Then -5 is represented by 24
5 = 11 or 10112.
Ex. Let b=10 and D=5. Then the 10s complement
of 12389 = 105 12389 = 100000 12389 =
87611.
2s complement
(radix complement)
Let x be a non-negative number. N N
Then x is represented by bD+(- 0 0000 0000
x). 1 0001 1111
Ex. Let b=2 and D=4. Then -1 is 2 0010 1110
represented by 24-1 = 15 or
3 0011 1101
11112.
Ex. Let b=2 and D=4. Then -5 is 4 0100 1100
represented by 24 5 = 11 or 5 0101 1011
10112. 6 0110 1010
Shortcut for base 2? 7 0111 1001
2s complement
(radix complement)
Shortcut for base 2? N N
Yes! Flip the bits and add 1. 0 0000 0000
1 0001 1111
2 0010 1110
3 0011 1101
4 0100 1100
5 0101 1011
6 0110 1010
7 0111 1001
2s complement
(radix complement)
Are all combinations of 4 bits N N
used? 0 0000 0000
No. (Now we only have one 1 0001 1111
zero.)
2 0010 1110
1000 is missing!
What is 1000?
3 0011 1101
Is it positive or negative? 4 0100 1100
Does -8 + 1 = -7 work in 2s 5 0101 1011
complement?
6 0110 1010
7 0111 1001
excess bD-1 (biased representation)
For pos, neg, and 0, x is represented by

bD-1 + x

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.
excess bD-1
For pos, neg, and 0, x is N N
represented by 0 1000 1000
bD-1 + x. 1 1001 0111
Ex. Let b=2 and D=4. Then the 2 1010 0110
excess 8 (24-1) representation 3 1011 0101
for 0 is 8+0 = 8 or 10002.
4 1100 0100
Ex. Let b=2 and D=4. Then
5 1101 0011
excess 8 for -1 is 8 1 = 7 or
01112. 6 1110 0010
7 1111 0001
2s complement vs. excess bD-1
In 2s, positives start with 0; in N N
excess, positives start with 1. 0 1000 1000
1 1001 0111
Both have one zero (positive). 2 1010 0110
3 1011 0101
Remaining bits are the same. 4 1100 0100
5 1101 0011
6 1110 0010
7 1111 0001
Summary of methods for
representing signed ints.
signedMag 1sComp 2 sComp excess 8
N n n n n n n
0 0000 1000 1111 0000 1000 1000
1 0001 1001 1110 1111 0111 1001
2 0010 1010 1101 1110 0110 1010
3 0011 1011 1100 1101 0101 1011
4 0100 1100 1011 1100 0100 1100
5 0101 1101 1010 1011 0011 1101
6 0110 1110 1001 1010 0010 1110
7 0111 1111 1000 1001 0001 1111
1000=-8| 0000 unused
Signed magnitude
1s complement
2s complement
Excess K (biased)

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.

bits Intel signed


8 sbyte -27..+27-1
16 sword -215..+215-1
32 sdword -231..+231-1
64 sqword -263..+263-1

In general, for k bits, the signed range is [-2k-1..+2k-1-1].


So where does the extra negative value come from?
Representing numbers (ints)
n n
Fixed, finite number of bits. 0 0000 0000
1 0001 1111
bits Intel signed 2 0010 1110
8 sbyte -27..+27-1
16 sword -215..+215-1
3 0011 1101
32 sdword -231..+231-1 4 0100 1100
64 sqword -263..+263-1 5 0101 1011
6 0110 1010
In general, for k bits, the signed range is
[-2k-1..+2k-1-1]. 7 0111 1001
So where does the extra negative value 8 1000
come from?
Addition of 2s complement binary
numbers
Consider 8-bit 2s complement binary
numbers.
Then the msb (bit 7) is the sign bit. If this bit is 0,
then this is a positive number; if this bit is 1, then
this is a negative number. 1 1 1
Addition of 2 positive numbers.
0 01 0 1000
Ex. 40 + 58 = 98
0 0111010
01100010
Addition of 2s complement binary
numbers
Consider 8-bit 2s
complement binary
numbers.
Addition of a negative to a
positive. 11 11
10101000
What are the values of these 2
terms? 01111010
-88 and 122
-88 + 122 = 34
1 00100010
So how can we perform subtraction?
Addition of 2s complement binary
numbers
Consider 8-bit 2s complement binary
numbers.
Subtraction is nothing but addition of the 2s
complement.
Ex. 58 40 = 58 + (-40) = 18 1 1 11

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.

If A and B are of the same sign but the result is of


the opposite sign, then overflow has occurred
(and the answer is therefore incorrect).

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.)

Você também pode gostar