I am having major trouble understanding the conversion of decimal - binary. I keep on relying on hints to get it correct in the JavaScript course but I want to learn how to do it by myself. Can someone explain to me how to convert decimal to binary and vice-versa in a way I can actually understand? Thanks!

i wonder if you already had seen this or not, either way happy reading https://www.cuemath.com/numbers/binary-to-decimal/

I’ll try to give you a simple explanation.

EXAMPLE:

A decimal (base 10) number can be broken down into ones, tens, hundreds, thousands etc. Each digit in a decimal number can have any of the ten values from 0-9.

So, the decimal number **12345** above is equivalent to:

**1 x 10,000 + 2 x 1000 + 3 x 100 + 4 x 10 + 5 x 1**

A binary (base 2) works exactly the same except that the multiple is 2 instead of 10. So the number can be broken down into ones, twos, fours, eights, sixteens etc. Each digit in a binary number can have either of the two values 0 or 1.

So, the binary number **10110** is equivalent to:

**1 x 16 + 0 x 8 + 1 x 4 + 1 x 2 + 0 x 1**.

(Or simply add the orange numbers together, where a 1 appears).

**10110** as a decimal would therefore be 22.

Counting in binary:

0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001 etc…

Decimal equivalent:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9 etc…

Thanks for this explanation of Binary to Decimal. But i’d also like to know how it works from Decimal to Binary =)

This is a little more involved, but not too complicated, as it can be broken down into a series of simple steps which you repeat until you arrive at the answer.

You will build your binary number from right to left (i.e. the ones digit, then twos, then fours etc).

- If the decimal number is odd, set the binary digit as 1 and subtract 1 from decimal. Else if even, set as 0.
- Divide the decimal number by 2.

Repeat these two steps until your decimal number reaches 0.

Let’s convert **12345** to a binary number:

12345 is odd so first binary digit is **1**. Subtract 1 from decimal.

12344 / 2 = 6172. Even, so 2nd binary digit is **0**.

6172 / 2 = 3086 => **0**

3086 / 2 = 1543 => **1**. Subtract 1 from decimal.

1542 / 2 = 771 => **1**

770 / 2 = 385 =>**1**

384 / 2 = 192 => **0**

192 / 2 = 96 => **0**

96 / 2 = 48 => **0**

48 / 2 = 24 => **0**

24 / 2 = 12 => **0**

12 / 2 = 6 => **0**

6 / 2 = 3 => **1**

2 / 2 = 1 => **1** … subtract 1 and we’re at 0.

Finally, read the binary numbers from bottom to top to get the answer:

**12345** in decimal is equivalent to **11000000111001** in binary.

Hopefully, you can see how simple this would be to code as a conversion algorithm.

Thank you, I finally understand how to do binary - decimal conversion. =D