Place values in binary
Decimal numbers use ten digits, and each position is worth ten times the position to its right: ones, tens, hundreds. Binary works the same way with two digits, 0 and 1, so each position is worth twice the one to its right. The rightmost bit is worth 1, the next 2, then 4, 8, 16 and so on, always doubling.
Eight bits make a byte, and the place values of a byte are worth memorizing, because they appear everywhere in computing, from file sizes to network masks.
| Bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Binary to decimal step by step
Take 11010110 as an example. Write the place values above or below the digits, starting with 1 on the right. Then add up only the place values that have a 1 under them: 128 + 64 + 16 + 4 + 2 = 214. That is the whole method, and it works for numbers of any length.
For long numbers, split the bits into groups of four from the right and convert each group separately into hex, then to decimal, or simply paste the number into the converter, which works with any length and does not round.
| Binary | Calculation | Decimal |
|---|---|---|
| 101 | 4 + 1 | 5 |
| 1111 | 8 + 4 + 2 + 1 | 15 |
| 100000 | 32 | 32 |
| 11010110 | 128 + 64 + 16 + 4 + 2 | 214 |
| 11111111 | 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 | 255 |
| 11111101010 | 1024 + 512 + 256 + 128 + 64 + 32 + 8 + 2 | 2026 |
Try it here: Binary to Decimal
Open the full tool pageDecimal to binary: divide by two
The reverse direction uses repeated division. Divide the number by 2, write down the remainder (0 or 1), and continue with the result until it reaches 0. The remainders read from the last to the first give the binary number.
For 214: 214 / 2 = 107 remainder 0, 107 / 2 = 53 remainder 1, 53 / 2 = 26 remainder 1, 26 / 2 = 13 remainder 0, 13 / 2 = 6 remainder 1, 6 / 2 = 3 remainder 0, 3 / 2 = 1 remainder 1, 1 / 2 = 0 remainder 1. Reading the remainders from the bottom up gives 11010110, the number we started with above.
Another way is to subtract the largest place value that fits: 214 minus 128 leaves 86, minus 64 leaves 22, minus 16 leaves 6, minus 4 leaves 2, minus 2 leaves 0. Write a 1 for every place value you used and a 0 for the others.
Hex: four bits per digit
Long binary numbers are hard to read, so programmers write them in hexadecimal. Hex has sixteen digits, 0 to 9 and A to F, and one hex digit stands for exactly four bits. To convert, split the binary number into groups of four from the right and replace each group using the table. 1101 0110 becomes D6, and D6 in decimal is 13 x 16 + 6 = 214.
| Binary | Hex | Decimal | Binary | Hex | Decimal |
|---|---|---|---|---|---|
| 0000 | 0 | 0 | 1000 | 8 | 8 |
| 0001 | 1 | 1 | 1001 | 9 | 9 |
| 0010 | 2 | 2 | 1010 | A | 10 |
| 0011 | 3 | 3 | 1011 | B | 11 |
| 0100 | 4 | 4 | 1100 | C | 12 |
| 0101 | 5 | 5 | 1101 | D | 13 |
| 0110 | 6 | 6 | 1110 | E | 14 |
| 0111 | 7 | 7 | 1111 | F | 15 |
Negative numbers: two's complement
Computers store negative whole numbers in two's complement. The top bit of a fixed width number carries a negative weight: in 8 bits the leftmost bit is worth -128 instead of 128. So 11111111 is -128 + 127 = -1, and 10000000 is -128.
To find the pattern of a negative number, write the positive value in binary, flip every bit and add 1. For -6 in 8 bits: 6 is 00000110, flipped 11111001, plus 1 gives 11111010. The same bits read as an unsigned number are 250, which is why it matters whether a value is signed. In the converter, turn on Signed and choose the bit width to see both readings.
Binary fractions
Digits after the binary point are worth one half, one quarter, one eighth and so on. 0.101 is 0.5 + 0.125 = 0.625. Many simple decimal fractions cannot be written exactly in binary: 0.1 becomes 0.000110011001100... with 0011 repeating forever. That is the reason 0.1 + 0.2 does not give exactly 0.3 in most programming languages. The converter shows such repeating digits in brackets, for example 0.0(0011).