Consider a decimal integral value, such as 5623. We intuitively understand that these digits mean (5 * 1000) + (6 * 100) + (2 * 10) + (3 * 1). Because there are 10 decimal numbers, the value of each subsequent digit to the left increases by a factor of 10.
Binary numbers work the same way, except because there are only 2 binary digits (0 and 1), the value of each digit increases by a factor of 2. Just like commas are often used to make a large decimal number easy to read (e.g. 1,427,435), we often write binary numbers in groups of 4 bits to make them easier to read (e.g. 1101 0101).
The following table counts to 15 in decimal and binary:
| Decimal Value | Binary Value |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 10 |
| 3 | 11 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |
| 8 | 1000 |
| 9 | 1001 |
| 10 | 1010 |
| 11 | 1011 |
| 12 | 1100 |
| 13 | 1101 |
| 14 | 1110 |
| 15 | 1111 |
Converting binary to decimal
In the following examples, we assume that we’re dealing with unsigned integers.
Consider the 8 bit (1 byte) binary number 0101 1110. Binary 0101 1110 means (0 * 128) + (1 * 64) + (0 * 32) + (1 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (0 * 1). If we sum up all of these parts, we get the decimal number 64 + 16 + 8 + 4 + 2 = 94.
Here is the same process in table format. We multiply each binary digit by its digit value (determined by its position). Summing up all these values gives us the total.
Converting 0101 1110 to decimal:
| Binary digit | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 0 |
| * Digit value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| = Total (94) | 0 | 64 | 0 | 16 | 8 | 4 | 2 | 0 |
Let’s convert 1001 0111 to decimal:
| Binary digit | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 1 |
| * Digit value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| = Total (151) | 128 | 0 | 0 | 16 | 0 | 4 | 2 | 1 |
1001 0111 binary = 151 in decimal.
This can easily be extended to 16 or 32 bit binary numbers simply by adding more columns. Note that it’s easiest to start on the right end, and work your way left, multiplying the digit value by 2 as you go.
Method 1 for converting decimal to binary
Converting from decimal to binary is a little more tricky, but still pretty straightforward. There are a few good methods to do this.
In this first method, you continually divide by 2, and write down the remainders. The binary number is constructed at the end from the remainders, from the bottom up.
Converting 148 from decimal to binary (using r to denote a remainder):
148 / 2 = 74 r0
74 / 2 = 37 r0
37 / 2 = 18 r1
18 / 2 = 9 r0
9 / 2 = 4 r1
4 / 2 = 2 r0
2 / 2 = 1 r0
1 / 2 = 0 r1
Writing all of the remainders from the bottom up: 1001 0100
148 decimal = 1001 0100 binary.
You can verify this answer by converting the binary back to decimal:
(1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 148
This method is the best for humans, as it only involves dividing by 2. It is less good for machines because it requires storing all of the bits as they are calculated so they can be printed in reverse order later.
Method 2 for converting decimal to binary
In the remaining two methods, we’ll work forwards, calculating each bit as we go, so that we don’t have to reconstruct the binary number at the end.
Consider the decimal number 148 again. The largest power of 2 that’s smaller than 148 is 128, so we’ll start there.
Is 148 >= 128? Yes, so the 128 bit must be 1. 148 - 128 = 20, which means we need to find bits worth 20 more.
Is 20 >= 64? No, so the 64 bit must be 0.
Is 20 >= 32? No, so the 32 bit must be 0.
Is 20 >= 16? Yes, so the 16 bit must be 1. 20 - 16 = 4, which means we need to find bits worth 4 more.
Is 4 >= 8? No, so the 8 bit must be 0.
Is 4 >= 4? Yes, so the 4 bit must be 1. 4 - 4 = 0, which means all the rest of the bits must be 0.
148 = (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 1001 0100
In table format:
| Binary number | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 |
| * Digit value | 128 |
