Integers
Binary is used to represent whole numbers so that they can be understood by the processor.
An integer:
- is a whole number
- can be a positive number or a negative number
The number 173 is an integer. It can be represented in binary as:
10101101
Binary works using a number system that is based on powers of 2. To understand why 10101101 is the same as 173 look at the following table:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| (27) | (26) | (25) | (24) | (23) | (22) | (21) | (20) |
| 1 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
| ON | OFF | ON | OFF | ON | ON | OFF | ON |
| 128 |
| 64 |
| 32 |
| 16 |
| 8 |
| 4 |
| 2 |
| 1 |
| (27) |
| (26) |
| (25) |
| (24) |
| (23) |
| (22) |
| (21) |
| (20) |
| 1 |
| 0 |
| 1 |
| 0 |
| 1 |
| 1 |
| 0 |
| 1 |
| ON |
| OFF |
| ON |
| OFF |
| ON |
| ON |
| OFF |
| ON |
When adding up all of the values that are on, the following sum is created:
128 + 32 + 8 + 4 + 1 = 173
In this example we are using 8 bits to represent a number. If we use 8 bits we can represent any number between 0 and 255.
If all of the values are off, the number is 0:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| (27) | (26) | (25) | (24) | (23) | (22) | (21) | (20) |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| OFF | OFF | OFF | OFF | OFF | OFF | OFF | OFF |
| 128 |
| 64 |
| 32 |
| 16 |
| 8 |
| 4 |
| 2 |
| 1 |
| (27) |
| (26) |
| (25) |
| (24) |
| (23) |
| (22) |
| (21) |
| (20) |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| OFF |
| OFF |
| OFF |
| OFF |
| OFF |
| OFF |
| OFF |
| OFF |
If all of the values are on, the number is 255:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| (27) | (26) | (25) | (24) | (23) | (22) | (21) | (20) |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| ON | ON | ON | ON | ON | ON | ON | ON |
| 128 |
| 64 |
| 32 |
| 16 |
| 8 |
| 4 |
| 2 |
| 1 |
| (27) |
| (26) |
| (25) |
| (24) |
| (23) |
| (22) |
| (21) |
| (20) |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| ON |
| ON |
| ON |
| ON |
| ON |
| ON |
| ON |
| ON |
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
Range of binary numbers
To figure out the range of numbers that can be stored with a set number of bits, use the following formula:
2n - 1
The reason for taking one away is because the integer 0 needs to be stored.
28 = 256
28 - 1 = 255
This means that the range of integers that can be represented using 8 bits is 0 – 255. Typically we categorise binary in groups of 8 bits (or 1 byte).
| Number of bits | Formula | Range |
| 8 | 28 - 1 | 0 - 255 |
| 16 | 216 - 1 | 0 - 65,535 |
| 24 | 224 - 1 | 0 - 16,777,215 |
| 32 | 232 - 1 | 0 - 4, 294,967,295 |
| Number of bits | 8 |
|---|---|
| Formula | 28 - 1 |
| Range | 0 - 255 |
| Number of bits | 16 |
|---|---|
| Formula | 216 - 1 |
| Range | 0 - 65,535 |
| Number of bits | 24 |
|---|---|
| Formula | 224 - 1 |
| Range | 0 - 16,777,215 |
| Number of bits | 32 |
|---|---|
| Formula | 232 - 1 |
| Range | 0 - 4, 294,967,295 |