Main content

## Computers and the Internet

# Converting decimal numbers to binary

AP.CSP:

DAT‑1 (EU)

, DAT‑1.C (LO)

, DAT‑1.C.1 (EK)

, DAT‑1.C.2 (EK)

, DAT‑1.C.4 (EK)

, DAT‑1.C.5 (EK)

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?(9 votes)
- 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(33 votes)

- 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.(4 votes)
- 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.(4 votes)

- how to write 10389810 in binary(4 votes)
- 1.0011110100010009⏨23 I used this program that I made to calculate that:

https://www.khanacademy.org/computer-programming/decimal-to-binary/5026660294639616(2 votes)

- I was wondering how would we represent numbers with decimal points like 6.5 or 8.0001 or something?(2 votes)
- Numbers with decimal points are represented as floating point, which stores the number in a format similar to scientific notation.(3 votes)

- whats a good techneqe for longer numbers(1 vote)
- Is there an end?(1 vote)
- No, you can keep adding bytes within the bit (8 bits = 1 byte) and the number will just keep adding every time.(1 vote)

- How do you convert greater values?(0 votes)
- Suppose you want to convert the integer x to binary.

1) Find the greatest integer, n, such that 2^n is less than x. (This can be calculated using n = log_2(x) rounded down.) Subtract 2^n from x and mark 1 for the binary representation.

2) If 2^(n-k) is less than the remaining value of x, subtract 2^(n-k) from x and mark 1 for the binary representation. If 2^(n-k) is greater than x, mark 0 for the binary representation.

3) Repeat step 2 for all values of k (in order) where k = [1, 2, ..., n].(1 vote)

- hurry up bruhhh i got work to doooo(0 votes)
- dawg 💀💀💀💀💀 just skip around(2 votes)

## 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.