If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

### Course: AP®︎/College Computer Science Principles>Unit 1

Lesson 2: Binary numbers

# Converting decimal numbers to binary

Learn a technique for converting decimal numbers into binary numbers using just pen, paper, and calculations. Works best for small numbers, since bigger numbers require increasingly more calculations. Created by Pamela Fox.

## Want to join the conversation?

• Is there a good technique for large numbers?
• The simplest method is to just divide (floor division) by two and keeping track of the remainder. After we're done we read the remainder from top to bottom.
So if you want to convert the decimal 5364 to binary

5364 / 2 = 2682 | 0
2682 / 2 = 1341 | 0
1341 / 2 = 670 | 1
670 / 2 = 335 | 0

335 / 2 = 167 | 1
167 / 2 = 83 | 1
83 / 2 = 41 | 1
41 / 2 = 20 | 1

20 / 2 = 10 | 0
10 / 2 = 5 | 0
5 / 2 = 2 | 1
2 / 2 = 1 | 0

1 / 2 = 0 | 1

So 5364 converts to the binary string
1 0100 1111 0100
• How would we write a program to convert decimal numbers to binary and/or vice versa? I don't need code, just a basic idea.
• Suppose we want to convert the number x into binary.

1) Find the largest power of 2 that is less than or equal to x and subtract it from x. Mark a 1 down for the binary representation.
2) See if the next smallest power of two is less than or equal to the remainder of x. If it is less than or equal, subtract it from x and mark a 1 down for the binary representation. If the power of two is greater than the remainder of x, mark a 0 down for the binary representation.
3) Repeat step 2 while the remainder of x is greater than 0.
• how to write 10389810 in binary
• I was wondering how would we represent numbers with decimal points like 6.5 or 8.0001 or something?
• Numbers with decimal points are represented as floating point, which stores the number in a format similar to scientific notation.
• Good video, is there an easier way?
• The Secret coders book
(1 vote)
• how wow why
who
• To convert a decimal number to binary, you need to follow these steps:

1. Divide the decimal number by 2 and write down the integer result (ignore the remainder).
2. Repeat step 1 with the integer result until you get 0.
3. Write down the remainders (in reverse order) of each division as 0 or 1 to get the binary equivalent.

For example, let's convert the decimal number 23 to binary:

1. 23 ÷ 2 = 11, remainder 1
2. 11 ÷ 2 = 5, remainder 1
3. 5 ÷ 2 = 2, remainder 1
4. 2 ÷ 2 = 1, remainder 0
5. 1 ÷ 2 = 0, remainder 1

The binary equivalent of 23 is therefore 10111.
• I really love this video I think it helps me understand it more