Skip to content
Programming Programming basics
All classes Intermediate About 15 min Premium

From decimal to binary

Choose a number and run the algorithm that turns it into binary: divide by 2, keep the remainder as the next bit, and repeat until nothing is left. Watch the bits build up from the right and count how many a number needs.

Core concept

Repeatedly dividing a number by 2 gives its binary digits as the remainders, from the last bit to the first; each remainder is placed one place further left

Topic: Programming basics

We count in decimal, with ten digits, 0 to 9. Computers store everything with only two, 0 and 1, called bits: binary: Counting with only two digits, 0 and 1. Computers store everything this way. All key words. To turn a decimal number into binary there is a simple algorithm: A list of clear steps that solves a problem, like a recipe. All key words. Divide the number by 2: the remainder, 0 or 1, is the last bit. Then divide what is left by 2 again: that remainder is the bit before it. Keep going until nothing is left. The program builds the answer as it goes: each new bit goes one place further left, so it is multiplied by place, which starts at 1 and grows 1, 10, 100, 1000… For 13 the remainders are 1, 0, 1, 1, so 13 in binary is 1101: 8 + 4 + 0 + 1.

Key words explained

What you will learn

  • Turn a decimal number into binary by dividing by 2.
  • Explain why the first remainder is the last bit.
  • Trace a while loop with % 2 and floor(n / 2).
  • Check a binary number by adding up its place values.

Before you start

  • Know place value in decimal, and remainders.

Materials

  • A notebook to write the remainders

Equipment

  • None

Procedure

  1. Step through the program for 13.
  2. Run it for 5, 6 and 25.
  3. Run it for 100 and count the bits.
  4. Answer the practice challenges.
Premium experiment Create a free account