Using the binary number system we can express
unsigned integers:
1
2 is 2
0 = 1
10
10
2 is 2
1 = 2
10
101
2 is 2
2 + 2
0 = 4 + 1 = 5
10
...
1000 0000
2 is 2
7 = 128
10
We know the place values for 8 bits
UNSIGNED are:
128 64 32 16 8 4 2 1
--- -- -- -- - - - -
2
7 2
6 2
5 2
4 2
3 2
2 2
1 2
0
So: 1 0 0 0 1 0 1 0 = 128 + 8 + 2 = 138
10
We also know this number can be represented in hex as 8A
16 and in octal as 212
8.
We also know that we can represent 256 integers in 8 bits and the largest integer is 255.
SIGNED INTEGERS
There are multiple schemes to represent negative numbers including:
Sign Magnitude One's Complement Two's Complement
000 = +0 000 = +0 000 = +0
001 = +1 001 = +1 001 = +1
010 = +2 010 = +2 010 = +2
011 = +3 011 = +3 011 = +3
100 = -0 100 = -3 100 = -4
101 = -1 101 = -2 101 = -3
110 = -2 110 = -1 110 = -2
111 = -3 111 = -0 111 = -1
Issues: balance, number of zeros, ease of operations
Which one is best? Why?
TWOS COMPLEMENT NUMBERS
To express negative numbers, the scheme of the binary positional number system is changed slightly
- with a large impact.
Using n bits, the
nth bit is simply changed from positive to negative so that it now represents -(2
n-1) .
The place values for n=8 bits
SIGNED are:
-128 64 32 16 8 4 2 1
--- -- -- -- - - - -
-2
7 2
6 2
5 2
4 2
3 2
2 2
1 2
0
The high bit becomes the "sign bit" only because, if set, it indicates the number is negative.
The high bit actually represents -(2
n-1).
Interestingly, n '1' bits (111..111) is ALWAYS -1 decimal for any n.
Why?
COMPARE THESE TWO REPRSENTATIONS
-128 64 32 16 8 4 2 1
1 0 0 0 1 0 1 0 = -128 + 8 + 2 = -118
10
This number can be represented in hex as 8A
16 and in octal as 212
8.
128 64 32 16 8 4 2 1
1 0 0 0 1 0 1 0 = 128 + 8 + 2 = 138
10
This number can be represented in hex as 8A
16 and in octal as 212
8.
WAIT! - both numbers have the same representation!.
How do we know if an 100010102 is -118 or 138? Which is it?
How do we know if an 8A16 is -118 or 138? Which is it?
ADDITION EXAMPLE:
When we add 0000 0001
0111 1111
---------
We get 1000 0000
If these are 8 bit UNSIGNED numbers that means 1 + 127 = 128. OK.
If these are 8 bit SIGNED numbers that means 1 + 127 = -128! NOT OK.
The machine can NOT differentiate. It is up to you, the programmer, to understand and interpret correctly.
We will look at the Carry (unsigned) and Overflow (signed) Flags that will help us detect these situations.
NEGATING NUMBERS
Numbers using the two's complement scheme can be negated by "taking the two's complement" of the number.
The process is two steps:
1. flip the bits (ones complement)
2. add one
0000 1111 = 15 = 8 + 4 + 2 + 1
Negate:
1. flip the bits: 1111 0000
2. add 1 : 1111 0001
1111 0001 = -128 + 64 + 32 + 16 + 1 = -15
Binary |
Decimal Unsigned |
Decimal Signed |
a. 1111 1111 |
255 |
-1 |
b. 0000 0000 |
0 |
0 |
c. 1000 0000 |
128 |
-128 |
d. 0111 1111 |
127 |
127 |
e. 1000 0001 |
129 |
-127 (note: flip bits and add 1 from d. above) |
f. 1000 1111 |
143 |
-113 |
g. 0000 1111 |
15 |
15 |
h. 1111 0001 |
241 |
-15 (note: flip bits and add 1 from g. above) |
N > 8 BITS
n = 16
======
Nothing changes - an elegant scheme.
If n = 16, the nth bit (2
(n-1) ) becomes -32,768
-32768 .. 128 64 32 16 8 4 2 1
----- --- -- -- -- - - - -
2
15 .. 2
7 2
6 2
5 2
4 2
3 2
2 2
1 2
0
1 .. 0 0 0 0 0 0 1 0 = -32768 + 2 = -32766
16 bit signed numbers (2s complement):
0000 0000 0000 0000 = 0
0000 0000 0000 0001 = +1
0000 0000 0000 0010 = +2
0000 0000 0000 0110 = +3
...
0111 1111 1111 1110 = +32,766
1111 1111 1111 1111 = +32,767
1000 0000 0000 0000 = -32,768
1000 0000 0000 0001 = -32,767
1000 0000 0000 0010 = -32,766
...
1111 1111 1111 1101 = -3
1111 1111 1111 1110 = -2
1111 1111 1111 1111 = -1
n = 32
======
If n = 32, the nth bit (2
(n-1) ) becomes -2,147,483,648
32 bit signed numbers (2s complement):
0000 0000 0000 0000 0000 0000 0000 0000 = 0
0000 0000 0000 0000 0000 0000 0000 0001 = +1
0000 0000 0000 0000 0000 0000 0000 0010 = +2
0000 0000 0000 0000 0000 0000 0000 0110 = +3
...
0111 1111 1111 1111 1111 1111 1111 1110 = +2,147,483,646
0111 1111 1111 1111 1111 1111 1111 1111 = +2,147,483,647
1000 0000 0000 0000 0000 0000 0000 0000 = -2,147,483,648
1000 0000 0000 0000 0000 0000 0000 0001 = -2,147,483,647
1000 0000 0000 0000 0000 0000 0000 0010 = -2,147,483,646
...
1111 1111 1111 1111 1111 1111 1111 1101 = -3
1111 1111 1111 1111 1111 1111 1111 1110 = -2
1111 1111 1111 1111 1111 1111 1111 1111 = -1