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.

AP®︎/College Computer Science Principles

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.
• whats a good techneqe for longer numbers
(1 vote)
• I really love this video I think it helps me understand it more
(1 vote)
• I have no questions
• how wow why
who
• solo dire que el video estuvo bueno