OCR H446, section 1.4.1 (b)
Computers store everything as binary: patterns of bits, each of which is 0 or 1. A bit is the smallest unit of data, and a byte is 8 bits. This page covers positive whole numbers (unsigned integers).
Place values
In denary (base 10), each place is worth 10 times the one to its right: 1, 10, 100, 1000. In binary (base 2), each place is worth 2 times the one to its right:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
The rightmost bit is the least significant bit (LSB), and the leftmost is the most significant bit (MSB).
Binary to denary
Add up the place values wherever there is a 1.
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 |
(The small subscript shows the base.)
Denary to binary
Method 1: subtract place values
Work from the largest place value down. Write a 1 if it fits (and subtract it), otherwise a 0.
Convert 77.
| Place | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Fits? | no | yes (left 13) | no | no | yes (left 5) | yes (left 1) | no | yes (left 0) |
| Bit | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 |
Method 2: divide by 2 repeatedly
Divide by 2, write down the remainder, and repeat with the quotient until you reach 0. Read the remainders from the bottom up.
| Divide | Quotient | Remainder |
|---|---|---|
| 38 | 1 | |
| 19 | 0 | |
| 9 | 1 | |
| 4 | 1 | |
| 2 | 0 | |
| 1 | 0 | |
| 0 | 1 |
Reading upwards: , which is in 8 bits (leading zero added).
The range of an unsigned number
With bits there are different patterns, so the values run from 0 up to .
| Bits | Number of values | Largest value |
|---|---|---|
| 4 (a nibble) | 15 | |
| 8 (a byte) | 255 | |
| 16 | 65 535 | |
| 32 | 4 294 967 295 |
The largest value is when every bit is 1: . It is , not , because 0 uses one of the patterns.
If you try to store a number larger than the maximum, the result overflows (see 1.4.1 (d)).
How many bits are needed?
To store a number , you need the smallest such that .
Example: how many bits to store 300? is too small, and is big enough, so 9 bits.
Useful tricks
- Odd or even: a binary number is odd if its last bit is 1.
- Doubling: shifting all the bits one place left (adding a 0 on the right) multiplies by 2. Shifting right divides by 2 (see 1.4.1 (i)).
- Powers of two have a single 1: 8 is , and 64 is .
Worked example
Task: (a) convert to denary. (b) Convert 200 to 8-bit binary. (c) What is the largest number that fits in 10 bits?
- (a) .
- (b) , so .
- (c) .
Exam tip: show your working (the place-value table, or the division table). Give the answer to the number of bits asked for, with leading zeros (such as 00001101). The largest value for bits is .
Check yourself
Convert to denary
.
Convert 100 to 8-bit binary
, so .
What is the largest unsigned number that can be stored in 12 bits?
.
How can you tell from its binary form whether a number is odd?
The least significant (rightmost) bit is 1.