This is the 3 bits (binary) "odometer" code:
Binary Odometer reading: 100 101 110 111 000 001 010 011 ----------------- +----+----+----+----+----+----+----+ Value represented: -4 -3 -2 -1 0 1 2 3 |
2s comp Represented
Code Value
========================
10000000 -128 <--- smallest negative value with 8 bits (-27)
10000001 -127
.....
11111000 -8
11111001 -7
11111010 -6
11111011 -5
11111100 -4
11111101 -3
11111110 -2
11111111 -1
00000000 0
00000001 1
00000010 2
00000011 3
00000100 4
00000101 5
00000110 6
00000111 7
00001000 8
.....
01111111 127 <--- largest positive value with 8 bits (27-1)
|
|
Property:
|
Let's see how we can encode a value in the 2s complement code
(Which is similar to the 10s complement example)
Absolute
2's compl Value Compare with: Value Binary number
================ =============================
10000000 -128 128 10000000
10000001 -127 127 01111111
.....
11111000 -8 8 00001000
11111001 -7 7 00000111
11111010 -6 6 00000110
11111011 -5 5 00000101 (11111011 = 100000000 - 00000101)
11111100 -4 4 00000100
11111101 -3 3 00000011
11111110 -2 2 00000010
11111111 -1 1 00000001
00000000 0
00000001 1 1 00000001
00000010 2 2 00000010
00000011 3 3 00000011
00000100 4 4 00000100
00000101 5 5 00000101
00000110 6 6 00000110
00000111 7 7 00000111
00001000 8 8 00001000
.....
01111111 127 127 01111111
|
We see that:
|
Note:
|
Later in this webpage, I will show you a quicker way to handle the negative values....
|
|
Example:
2s complement code c = 00010010
What value does this code represent ?
00010010 -> it is a positive value
the value = 00010010 in binary
Convert to decimal: 0 0 0 1 0 0 1 0
16 + 2 = 18
Value = 18
|
Here is a summary of the examples:
2s complement code Value represented by the code
-------------------- -------------------------------
00000111 7
11111001 -7
00010010 18
11101110 -18
|
Can you see the how the representation for x and −x are related ??
Take a close look at this table with more representations of positive and negative values:
Absolute
2's compl Value Compare with: Value Binary number
================ =============================
10000000 -128 128 10000000
10000001 -127 127 01111111
.....
11111000 -8 8 00001000
11111001 -7 7 00000111
11111010 -6 6 00000110
11111011 -5 5 00000101 (11111011 = 100000000 - 00000101)
11111100 -4 4 00000100
11111101 -3 3 00000011
11111110 -2 2 00000010
11111111 -1 1 00000001
00000000 0
00000001 1 1 00000001
00000010 2 2 00000010
00000011 3 3 00000011
00000100 4 4 00000100
00000101 5 5 00000101
00000110 6 6 00000110
00000111 7 7 00000111
00001000 8 8 00001000
.....
01111111 127 127 01111111
|
You can notice the following relationship between the representations for x and -x:
2s compl code(-x) = 10000000 - 2s complement code(x) |
So to negate a value represented in the 2s complement code:
|
What you may have learned is:
|
You probably have no idea why this works...
Let me explain this procedure using an example...
The binary representation for the value 7 in 8 bits is:
00000111 (= 7)
|
To find the 2s complement representation for −7, we subtract 00000111 from 100000000:
100000000
- 00000111 (= 7)
-----------
11111001 (= -7)
|
The subtraction can be broken up in 2 steps as follows:
100000000 - 00000111 = (1 + 11111111) - 00000111 = 1 + (11111111 - 00000111) [easy subtraction !] = 1 + 11111000 [result is same as flipping bits !] |
Subtraction using 11111111 will result in flipping the bits in the other binary number because you never have to borrow and 1 - 0 = 1 and 1 - 1 = 0 !!!
Summary: to negate a 2s complement representation:
Another example:
As you saw above: 00010010 represents +18
To get the representation for -18, you can do this:
(1) Flip each bit: 00010010 -> 11101101
(2) Add 1 to result: 11101101
+ 1
----------
11101110
which is - as you saw above - the representation for -18
|
Values 8 digit 2's compl repr
Adding 2
positive 5 00000101
values + 9 + 00001001
----- ----------
14 00001110 -> 8 + 4 + 2 = 14
Adding
positive + 5 00000101
negative + -9 + 11110111
----- ----------
-4 11111100 -> represents -4
Adding
negative + -5 11111011
positive + 9 + 00001001
----- ----------
4 00000100 -> represents 4
Adding 2
negative -5 11111011
values + -9 + 11110111
----- ----------
-14 11110010 -> represents -14
Values 8 digit 2's compl repr
Subtract 2
positive 5 00000101
values - 9 - 00001001
----- ----------
-4 11111100 -> represents -4
Subtract
positive - 5 00000101
negative - -9 - 11110111
----- ----------
14 00001110 -> represents 14
Subtract
negative - -5 11111011
positive - 9 - 00001001
----- ----------
-14 11110010 -> represents -14
Subtract 2
negative -5 11111011
values - -9 - 11110111
----- ----------
4 00000100 -> represents 4
How to run the program:
|
For example:
00000011 <---- representation for the value *** (3)
+ 00000101 <---- representation for the value ***** (5)
------------
00001000 <---- representation for the value ******** (8)
You can use this program to see why the program prints certain results: click here DEMO
How to run the program:
|
You can use any code, and you will still have the overflow problem....
Because...
|
So please don't blame the 2s complement code for the overflow phenomenon....
Blame it on the fact that we use 4 bytes to store an int typed variable !!!