Find a course

Module 2: Converting Binary and DecimalLesson 4 of 1112 min

Decimal to Binary

Convert decimal numbers to binary by subtracting powers of two or by repeated division by 2, and check results by converting back.

Step 1 of 13Learn

Method 1, subtract powers of two

The first method works like making change with coins worth 128, 64, 32, 16, 8, 4, 2 and 1. Start with the biggest coin that fits, take it, and continue with what's left.

Convert 77 to binary:

  1. The largest power of two that fits in 77 is 64. Write 1 in the 64s place. Remaining: 77 − 64 = 13.
  2. Does 32 fit in 13? No: write 0.
  3. Does 16 fit in 13? No: write 0.
  4. Does 8 fit? Yes: write 1. Remaining: 13 − 8 = 5.
  5. Does 4 fit in 5? Yes: write 1. Remaining: 1.
  6. Does 2 fit in 1? No: write 0.
  7. Does 1 fit? Yes: write 1. Remaining: 0.

Reading the bits from the 64s place down: 1001101.

Check: 64 + 8 + 4 + 1 = 77. Correct.

The key rule is to write a digit for every place from your starting power down to 1, including the zeros. Skipping zeros is the classic error: 64, 8, 4 and 1 written as just "1111" would mean 15, not 77.

This method is fast for numbers up to a few hundred, especially once you've memorized the powers of two. For a full byte, start at 128 and you'll always get eight digits.

Step 2 of 13Diagram

Making change for 45

Making change for 45Process diagram: 45 → Take 32, left 13 → Skip 16 → Take 8, left 5 → Take 4, left 1 → Skip 2 → Take 1, left 0 → 101101.45Take 32, left13Skip 16Take 8, left 5Take 4, left 1Skip 2Take 1, left 0101101

Tap a shape with to see more.

Check: 32 + 8 + 4 + 1 = 45.
45
Take 32, left 13

64 is too big, 32 fits: bit 1.

Skip 16

16 > 13: bit 0.

Take 8, left 5
Take 4, left 1
Skip 2
Take 1, left 0
101101

Step 3 of 13Practice · Work it out

What is 37 in binary? Enter the binary digits as a number, such as 1010.

Step 4 of 13Learn

Method 2, divide by 2

For bigger numbers, the division method is more mechanical and doesn't require knowing large powers of two. Repeatedly divide by 2, write down each remainder (always 0 or 1), and keep dividing the quotient until it reaches 0. Then read the remainders from bottom to top.

Convert 77 again:

DivisionQuotientRemainder
77 ÷ 2381
38 ÷ 2190
19 ÷ 291
9 ÷ 241
4 ÷ 220
2 ÷ 210
1 ÷ 201

Reading the remainders from the bottom up: 1001101. Same answer as before.

Why bottom to top? The first remainder tells you whether the number is odd or even, which is the ones bit, the rightmost one. Each later division reveals the next place to the left. So the first remainder you write is the last digit of the answer.

Reading the remainders top to bottom gives the number backward: 1011001, which is 89. That's the most common error with this method, so always check by converting back.

The same division method works for any base: to convert to base 16, divide by 16 and read remainders bottom to top.

Step 5 of 13Practice · Put in order

Use the division method to convert 13 to binary. Put the steps in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. 13 ÷ 2 = 6 remainder 1
  2. 6 ÷ 2 = 3 remainder 0
  3. 3 ÷ 2 = 1 remainder 1
  4. Read remainders bottom to top to get 1101
  5. 1 ÷ 2 = 0 remainder 1

Step 6 of 13Note

Step 7 of 13Practice · Fill in the blanks

Fill in the blanks

Using the division method, the remainders must be read from . The first remainder you calculate becomes the bit of the binary number.

Step 8 of 13Diagram

Dividing 22 by 2 again and again

Dividing 22 by 2 again and againProcess diagram: 22 ÷ 2 = 11 r 0 → 11 ÷ 2 = 5 r 1 → 5 ÷ 2 = 2 r 1 → 2 ÷ 2 = 1 r 0 → 1 ÷ 2 = 0 r 1 → Read up: 10110.22 ÷ 2 = 11 r 011 ÷ 2 = 5 r 15 ÷ 2 = 2 r 12 ÷ 2 = 1 r 01 ÷ 2 = 0 r 1Read up: 10110

Tap a shape with to see more.

16 + 4 + 2 = 22.
22 ÷ 2 = 11 r 0

This remainder is the LAST binary digit.

11 ÷ 2 = 5 r 1
5 ÷ 2 = 2 r 1
2 ÷ 2 = 1 r 0
1 ÷ 2 = 0 r 1

The final remainder is the FIRST binary digit.

Read up: 10110

Step 9 of 13Learn

Fitting numbers into bytes

Computers usually store numbers in fixed sizes: 8 bits, 16 bits, 32 bits or 64 bits. When a number needs fewer bits than the space available, it's padded with leading zeros. The number 5 stored in a byte is 00000101.

Before converting, it helps to know whether a number fits at all. Remember: n bits store values from 0 to 2n − 1.

  • Up to 255 fits in 8 bits.
  • Up to 65,535 fits in 16 bits.
  • 300 needs at least 9 bits, because 28 = 256 is too small and 29 = 512 is enough.

A quick way to find how many bits a number needs: find the first power of two greater than the number. For 300, that's 512 = 29, so 9 bits.

This connects to real limits. Many older systems stored values like years, scores or counters in a fixed number of bits, and when a value grew past the limit, strange things happened. You'll see exactly what in the overflow lesson. Programmers choose sizes carefully: a byte is plenty for a person's age, but far too small for the number of views on a popular video.

Step 10 of 13Practice · Diagram

Largest unsigned number each size can hold

Tap the smallest standard size that can store the number 1,000.

Tap the shape that answers the question, then press Check.

Largest unsigned number each size can holdScale: 8-bit: 255, 16-bit: 65,535, 32-bit: about 4.3 billion, 64-bit: about 18 quintillion.10²10⁵10⁸10¹¹10¹⁴10¹⁷10²⁰2558-bit: 25565,53516-bit: 65,5354,294,967,29532-bit: about 4.3billion1.84 × 10¹⁹64-bit: about 18quintillion
8-bit: 255

255

16-bit: 65,535

65,535

32-bit: about 4.3 billion

4,294,967,295

64-bit: about 18 quintillion

1.84 × 10¹⁹

Step 11 of 13Practice · Work it out

How many bits are needed to store the number 1,000 in binary?

Step 12 of 13Practice · Code challenge

Write toBinary(n) that converts a whole number n (0 or more) to a binary string, such as toBinary(13) returning "1101". Use the division method with % (remainder) and Math.floor(n / 2), not toString(2). toBinary(0) should return "0".

Runs in a sandbox in your browser.
  • toBinary(13)
  • toBinary(77)
  • toBinary(255)
  • toBinary(1)
  • toBinary(0)

Step 13 of 13Practice · Choose one

A student converts 26 with the division method and gets the remainders 0, 1, 0, 1, 1 in that order. What is 26 in binary?