DEV Community

Thecodetranslator
Thecodetranslator

Posted on

How to Convert Decimal to Binary by Hand (and Why 0.1 Never Ends)

Converting decimal to binary comes down to one repeated move: divide by 2 and keep the remainder. Here is the method, a second mental shortcut, and the edge cases (fractions, negatives, language built-ins) that catch people out.

The division method

Divide the number by 2 and write down the quotient and the remainder (always 0 or 1). Repeat with the quotient until it reaches 0. Then read the remainders from last to first.

Take 156:

Division Quotient Remainder
156 ÷ 2 78 0
78 ÷ 2 39 0
39 ÷ 2 19 1
19 ÷ 2 9 1
9 ÷ 2 4 1
4 ÷ 2 2 0
2 ÷ 2 1 0
1 ÷ 2 0 1

Read upward: 10011100. Check it by adding the places that hold a 1: 128 + 16 + 8 + 4 = 156. That is 8 bits, so it fits in one byte.

Why it works: each division asks whether the number is odd or even. Odd means a 1 in the 1s place. The rest is halved, and the same question now covers the 2s place, then the 4s. Remainders come out smallest place first, which is why you read them back upward.

The subtract-powers-of-2 method

Below 256 there is a faster mental route. List 128, 64, 32, 16, 8, 4, 2, 1 and ask, left to right, whether each one fits into what is left. If it does, write 1 and subtract. If not, write 0.

For 156: 128 fits (1), 28 left. 64 no, 32 no. 16 fits (1), 12 left. 8 fits (1), 4 left. 4 fits (1), 0 left. 2 and 1 no. That is 10011100 again.

Division wins for big numbers because you do not need a list of place values.

Quick reference

  • 13 = 1101
  • 64 = 1000000
  • 128 = 10000000
  • 255 = 11111111
  • 256 = 100000000

The binary number gains a digit every time you pass a power of 2.

Decimal fractions

Convert the whole part with division. Convert the fraction by multiplying by 2: the digit in front of the point is your next binary digit. Drop it, keep the rest, and multiply again until you hit 0.

For 0.625:

  • 0.625 × 2 = 1.25, digit 1, keep 0.25
  • 0.25 × 2 = 0.5, digit 0, keep 0.5
  • 0.5 × 2 = 1.0, digit 1, done

So 0.625 is 0.101, and 13.625 is 1101.101.

Why 0.1 never ends

Some fractions never reach 0. Run 0.1 and the digits go 0, 0, 0, 1, 1, 0, 0, 1, 1 and keep repeating: 0.0(0011). The reason is that 0.1 is 1/10, and 10 has a factor of 5 that is not a power of 2. Only fractions with a power-of-2 denominator (0.5, 0.25, 0.375, 0.625) end cleanly. Everything else gets cut off, which is why a computer stores 0.1 as a close neighbour, not the exact value.

Negative numbers

Binary hardware has no minus sign. Negatives are stored as two's complement. For -13 in 8 bits: write 13 as 00001101, flip every bit to get 11110010, then add 1. The result is 11110011.

In code

bin(13)              # '0b1101'
format(13, 'b')      # '1101'
format(13, '08b')    # '00001101'
Enter fullscreen mode Exit fullscreen mode
(13).toString(2)     // "1101"
Enter fullscreen mode Exit fullscreen mode
=DEC2BIN(9, 4)       // Excel: 1001
Enter fullscreen mode Exit fullscreen mode

bin() adds a 0b prefix and format() does not. Use '08b' to pad to 8 digits. Excel's DEC2BIN only takes whole numbers from -512 to 511, and returns negatives as 10-bit two's complement: =DEC2BIN(-100) gives 1110011100.

Common mistakes

  • Reading the remainders top to bottom. You get the number backwards: 13 would come out as 1011, which is 11.
  • Stopping at a quotient of 1. The last division, 1 ÷ 2, still gives a remainder of 1. That is your leftmost digit.
  • Dropping leading zeros when a width is asked for. 13 is 1101, but in 8 bits it is 00001101.

Check your work

I built a free converter that shows the division steps, handles negatives and decimal fractions, and runs entirely in your browser, so nothing you type is sent anywhere:

Decimal to Binary Converter

It also has a 0-32 table if you just need the small ones. Going the other way? See my post on converting binary to decimal.

Which part trips you up most: fractions, negatives, or the bit widths? Let me know in the comments.

Top comments (0)