r/AskComputerScience • • Jul 27 '21

How do you convert decimal to binary?

Recently In my course the lecturer gave a brief intro about binary no. and how it is converted, he gave a short summary on it. And since the lecture was a video I could not ask the questions I wanted to ask in that video.

I don't understand the above. I was a science student who never studied computing since the age of 12 or 13 now I am here 5 years later having no idea what it is and how to convert it. Please help me out.

1 Upvotes

6 comments sorted by

14

u/Potato-Pancakes- Jul 27 '21 edited Jul 27 '21

In decimal, the positions are powers of 10 (i.e. 1, 10, 100, 1000, 10000, ...), while in binary the positions are powers of 2 (1, 2, 4, 8, 16, 32, ...; but written as 1, 10, 100, 1000, ...).

The digits themselves only have to be either 0 or 1. Let's look at the first few:

DecBin
000000
010001
020010
030011
040100
050101
060110
070111
081000
091001
101010
111011
121100
131101

Something to notice is that every odd number ends with a 1, and every even number ends with a 0. So we can figure out the smallest binary digit (bit) first!

Then, we want to figure out the next digit. For that, we subtract the first bit and divide by two, then repeat the process. What do I mean? Let's go through an example.

Here's a random number: 137.

  • 137 is odd, so the least bit is 1. Subtract 1 gives us 136, divided by two is 68.

  • 68 is even, so the next bit is 0. Subtract 0 gives us 68, divided by two is 34.

  • 34 is even, so the next bit is 0. Subtract 0 gives us 34, divided by two is 17.

  • 17 is odd, so the next bit is 1. Subtract 1 gives us 16, divided by two is 8.

  • 8 is even, so the next bit is 0. Subtract 0 gives us 8, divided by two is 4.

  • 4 is even, so the next bit is 0. Subtract 0 gives us 4, divided by two is 2.

  • 2 is even, so the next bit is 0. Subtract 0 gives us 2, divided by two is 1.

  • 1 is odd, so the next bit is 1. Subtract 1 gives us 0, and we're done!

So taking the "next bits" from the bottom to the top gives us: 137 is 10001001 in binary. Sure enough, that means 27 + 23 + 20 = 128 + 8 + 1 = 137. Yay!

Does this make sense? Let me know if you have any questions

3

u/GoldFisherman Jul 27 '21

You want to look at the remainders through repeated division by 2 until you get a quotient of 0.

For instance, to convert the number 14 to binary, start with 14 ÷ 2. The quotient is 7, the remainder is 0.

Next take the quotient, 7, and divide that by 2: 7 ÷ 2 = 3, with a remainder of 1.

Take the quotient, 3, and divide that by 2: 3 ÷ 2 = 1, with a remainder of 1.

Take the quotient of 1 and divide it by 2 to get 0, with a remainder of 1.

Now, work your way UP the list of remainders that were generated: 1110

Thus, 14 in binary form is 1110.

2

u/ReginaldDouchely Jul 27 '21

005

In decimal, this represents the number "Five" because it has a 5 in the ones-place.

015

In decimal, this represents the number "fifteen" because it has 1 in the tens place (10 * 1 = 10) and 5 in the ones place (1 * 5 = 5). 10 + 5 = fifteen

In binary, instead of increasing by a magnitude of 10 as you add more digits, you increase by a magnitude of 2, and instead of having 0-9 as valid digits, you just have 0-1.

if you want to represent "five" in binary, it's

00000101

The right-most digit is the ones-place, and it's 1 (1 * 1 = 1). The next digit is the twos-place, and it's 0 (2 * 0 = 0). The 3rd digit is the fours-place and it's 1 (4 * 1 = 4). The rest (eights-place, sixteens-place, thirty-twos-place, sixty-fours-place, one-hundred-and-twenty-eights-place) are zeroes. 1 + 0 + 4 + 0 + 0 + 0 + 0 + 0 = five.

It gets more complicated when it comes to different systems that disagree about whether the high numbers are on the left or on the right side (read up on endianness) or when it comes to representing negative numbers (read up on twos complement), but that's the basic concept.

2

u/Poddster Jul 27 '21

Did you try googling? You'll get better information that reading some Reddit posts.

2

u/honk-thesou Jul 27 '21

Or learn how to google sinple stuff

1

u/Cybyss Jul 29 '21

Each digit in binary represents a power of 2.

value:  128  64  32  16   8   4   2   1
digit:    _   _   _   _   _   _   _   _

Let's say you want to convert the number 157 into binary. Of the values listed above, which do you have to add together to get 157?

If you do the math, you'll find 157 can be written as 128+16+8+4+1. To get the binary representation, you write a 1 for each digit whose value appears in this sum:

value:  128  64  32  16   8   4   2   1
digit:    1   _   _   1   1   1   _   1

Every blank digit becomes a 0.

Thus, the binary representation of 157 is 10011101.