Binary
To turn a denary number into unsigned 8-bit binary, you can use the place-value method: compare the number with each binary place value from largest to smallest, deciding one bit at a time.
Write 128, 64, 32, 16, 8, 4, 2 and 1 in a row. Each value is double the one on its right, and between them they cover every whole number from 0 to 255.
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 1 |
| Amount left | 89 | 25 | 25 | 9 | 1 | 1 | 1 | 0 |
At each place, ask whether that place value fits into the amount you still have left.
Take 89. 128 is too big, so write 0. 64 fits, so write 1 and 89 − 64 = 25. 32 is too big: 0. 16 fits: 1, leaving 9. 8 fits: 1, leaving 1. Then 4 and 2 are too big: 0 and 0. Finally 1 fits: 1, leaving 0. Reading across gives 01011001.
The subtraction is what keeps the running total right. Writing a 1 without subtracting would set extra bits and push the total past the number you started with.
Once the amount left reaches zero, fill every remaining place to the right with 0 so that the answer is exactly eight bits, even when those bits add nothing.
Eight bits give 256 different patterns, so an unsigned 8-bit number holds the values 0 to 255. Zero uses every bit at 0, so it is 00000000. 255 needs every place value: 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, so all eight bits are 1 and it is 11111111. A denary number above 255 needs a ninth bit, so it cannot be stored in an unsigned 8-bit number at all.
Repeated division is a way of turning a denary number into binary by dividing by 2 again and again. You divide by 2, write down the remainder, and keep dividing the whole-number answer until it reaches 0.
Divide 45 by 2 and note the remainder — the 0 or 1 left over. Then divide the whole-number answer (the quotient) by 2 and note that remainder too. Stop as soon as the quotient reaches 0, because there is nothing left to halve.
| Division | Quotient | Remainder |
|---|---|---|
| 45 ÷ 2 | 22 | 1 |
| 22 ÷ 2 | 11 | 0 |
| 11 ÷ 2 | 5 | 1 |
| 5 ÷ 2 | 2 | 1 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
The remainders arrive the wrong way round: the first one you find is the least significant bit, which belongs on the far right of the answer. So read the list from the bottom upwards: 1 0 1 1 0 1. That gives 101101, and 32 + 8 + 4 + 1 = 45, which checks the answer is correct.
101101 is only six bits long, so add leading zeros on the left until you have eight bits: 0010 1101. The zeros add no value, but they keep every bit sitting in the correct place value column. Smaller numbers need more padding — denary 5 comes out as 0000 0101 — while values of 128 or more already fill all eight places.
A byte can be split into two nibbles of four bits each. Each nibble is a small binary number in its own right, which makes a long pattern of 0s and 1s easier to read and convert.
The nibble on the left is the high nibble, holding the place values 128, 64, 32 and 16. The nibble on the right is the low nibble, holding 8, 4, 2 and 1. The place values still double from right to left across the whole byte — the split does not break the pattern.
Take the denary number 97. In the high nibble, 128 is too big, so the first bit is 0. The next place value, 64, fits, so that bit is 1 and 97 − 64 = 33 is left. Next, 32 fits into 33, so that bit is 1 and 33 − 32 = 1 is left. The 16 is too big for the 1 remaining, so that bit is 0. The high nibble is 0110.
Now the low nibble, starting with the 1 left over. The place values 8, 4 and 2 are too big, so they are 0; the final place value, 1, fits exactly, so the last bit is 1. The low nibble is 0001. Putting them together gives 0110 0001.
Writing the nibbles in the wrong order, low nibble first, gives a pattern that means a completely different number, so always build the high nibble on the left.