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: Computers and the Internet>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?
• 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.
• 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.
• Read secret coders. It helps teach binary. It makes a lot of sense
• I really love this video I think it helps me understand it more
• how about numbers like infity
(1 vote)
• Binary, like other number systems, works within finite bounds, so infinity cannot be represented in binary.
• how does numbers like 4.6 or -5 or -2.5 work in binary?
(1 vote)

## Video transcript

- [Instructor] Let's try to convert the decimal number six, from decimal to binary, I'm gonna show you my favorite way of doing it. So I started off by writing dashes for the bits. I'm gonna start off with eight dashes, representing the eight bits or one byte, even though we probably don't need all of these for such a small number. And then I'm going to write the values of each of these places. So this first bit, this is the ones place or two to zero. The second bit is the twos place, two to one. The third bit is the fours place, two squared. And then we have eights place, 16s place, you see we just double, 32s place, 64s place and 128s place. Okay, now that we have these places, I start on the left side, and I look at the place and I say, is this value greater than this value? 128 is greater than that value, so we're gonna put a zero here, because we do not need to represent the value 128 inside this tiny little number. 64 is also greater than six, 32 also greater than six, 16 is also greater, eight is also greater, so we've got a whole lot of zeros so far. Four is not greater than six. So we're finally going to put a one. And then what we're gonna do is subtract four from six. So six minus four equals two. So that's the remaining value that we still need to represent. And we go to the next one. This is the twos place, two is not greater than two, it's actually exactly equal to two. So we're going to put a one as well. And now subtract again, two minus two equals zero, we fix that, two minus two is zero, there's nothing left to represent, we have entirely represented the value six already. So that means we can put a zero in this remaining place. So now we can say this is how to represent six in binary. The full byte would look like this, or we might shorten it to just four bits. Or we might even shorten it to just three, but we typically do like present bits in groupings of four or eight. Now let's try a bigger number. So let's erase all this work here. I wanna keep my place values around because those are handy and they're gonna be the same, and just erase everything else, okay, good enough. All right, so let's try the value 25, okay? 25 decimal, how do we convert that to binary? So once again we start over here, is 128 greater than 25? Yes it is, put a zero. 64 is greater, put a zero. 32 also greater, we'll put a zero. 16 is not greater than 25, so 16 is contained within 25. We'll put a one and then do a little math to figure out what we still need to represent. So 25 minus 16 equals four and five, that's nine. All right, so we still need to represent the value nine in these remaining bits, okay? The next place value is the eights place. Eight is not greater than nine. So that means we are going to need to use the eights place, we'll put a one in there. So now we have nine minus eight equals one. All right, there's only one more thing that we have to represent. So we've already represented 24, right? Here we're looking at having represented 24. If we filled the rest of zeros right now, we'd have the number 24, but we're looking for 25. So we keep going, is four greater than one? Yes, so we'll put a zero. Two is greater than one, we'll put a zero. One is equal to one, so we will put a one here. So here we have the decimal number 25 in binary. So this required one, two, three, four five bits, so we would probably represent it in a byte like this. So this is the basic strategy that I use for converting numbers from decimal to binary and this will work for numbers up to 255 using these eight bits here. Beyond that, you're going to need more bits. And honestly, at that point, you might wanna just use a calculator or write a program to do it.