Representing negative integers
Negative numbers can also be represented in binary. The name of the system most commonly used to represent and handle negative numbers is 'Two's complement'. There are two common methods used to figure out how a negative number is stored using 2s complement.
Two's complement
The first technique involves three steps:
- Find the positive value
- Switch 1s and 0s
- Add 1
Example
To represent -116, the following steps would be followed:
1. Find positive 116.
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 |
| 128 |
| 64 |
| 32 |
| 16 |
| 8 |
| 4 |
| 2 |
| 1 |
| 0 |
| 1 |
| 1 |
| 1 |
| 0 |
| 1 |
| 0 |
| 0 |
01110100
2. Switch 1s and 0s
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 | 0 | 1 | 1 |
| 128 |
| 64 |
| 32 |
| 16 |
| 8 |
| 4 |
| 2 |
| 1 |
| 0 |
| 1 |
| 1 |
| 1 |
| 0 |
| 1 |
| 0 |
| 0 |
| 1 |
| 0 |
| 0 |
| 0 |
| 1 |
| 0 |
| 1 |
| 1 |
10001011
3. Add 1
To add one, it is necessary to start at the right hand side of the number. If the number on the right is a 1, move to the next number. Keep moving from right to left until you find a 0.
In this example there is a 0 underneath the number 4. This 0 would become a 1. The 1s that are to the right of the 0 that was found first need to change from being a 1 to a 0:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 1 | 0 | 0 | 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
| 128 |
| 64 |
| 32 |
| 16 |
| 8 |
| 4 |
| 2 |
| 1 |
| 1 |
| 0 |
| 0 |
| 0 |
| 1 |
| 0 |
| 1 |
| 1 |
| 1 |
| 0 |
| 0 |
| 0 |
| 1 |
| 1 |
| 0 |
| 0 |
10001100 = -116
Second technique
The second technique it to start with -128 in the leftmost column instead of starting with positive 128. This is how the computer would handle a Two's complement number. The most significant bit (leftmost bit) is treated as -128.
To calculate the binary value of a negative decimal number simply start at -128 and work your way back to the correct answer. In this example the value of -110 is represented as:
| -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 |
| -128 |
| 64 |
| 32 |
| 16 |
| 8 |
| 4 |
| 2 |
| 1 |
| 1 |
| 0 |
| 0 |
| 1 |
| 0 |
| 0 |
| 1 |
| 0 |
-128 + 16 + 2 = -110
This technique is easier to use if the person calculating the negative number is comfortable with starting at -128 and working back to the answer.
When using 8 bit Two's complement, the range of numbers that can be stored changes when compared to 8 bit storage of positive integers. This is because it is necessary to include the negative numbers within the range. The lowest value that can be represented with 8 bits is – 128 and the highest value is 127. This means the overall range is:
-128 to 127
(-2n-1 to 2n-1 - 1)
Typically, we categorise binary in groups of 8 bits (or 1 byte). The range of positive and negative numbers that can be represented using 8, 16, 24 and 32 bit 2’s complement is shown below.
| Number of bits | Formula | Range |
| 8 | -28-1 to (28-1) - 1 | -128 to 127 |
| 16 | -216-1 to (216-1) - 1 | -32,768 to 32,767 |
| 24 | -224-1 to (224-1) - 1 | -8,388,608 to 8,388,607 |
| 32 | -232-1 to (232-1) - 1 | -2,147,483,648 to 2,147,483,647 |
| Number of bits | 8 |
|---|---|
| Formula | -28-1 to (28-1) - 1 |
| Range | -128 to 127 |
| Number of bits | 16 |
|---|---|
| Formula | -216-1 to (216-1) - 1 |
| Range | -32,768 to 32,767 |
| Number of bits | 24 |
|---|---|
| Formula | -224-1 to (224-1) - 1 |
| Range | -8,388,608 to 8,388,607 |
| Number of bits | 32 |
|---|---|
| Formula | -232-1 to (232-1) - 1 |
| Range | -2,147,483,648 to 2,147,483,647 |