Published Jan 14 2020

Psssst! The 2023 WEB DEVELOPMENT BOOTCAMP is starting on FEBRUARY 01, 2023! SIGNUPS ARE NOW OPEN to this 10-weeks cohort course. Learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much more! ✨

I recently introduced the Decimal Number System, the one we are used as humans, and the Binary Number System, the one machines are used to.

In this tutorial I want to explain how to convert from decimal numbers to binary numbers.

We have a separate process for integers, and for fractions.

A decimal integer can be converted to binary by dividing it by 2.

Take the quotient, and keep dividing it by 2, until you reach zero.

Each time you perform this division, take note of the **remainder**. Now reverse the remainders list, and you get the number in binary form.

Let’s make an example, I want to convert 29 into binary:

\[29\div2 = 14\] remainder `1`

\[14\div2 = 7\] remainder `0`

\[7\div2 = 3\] remainder `1`

\[3\div2 = 1\] remainder `1`

\[1\div2 = 0\] remainder `1`

The binary number representing the 29 decimal is `11101`

.

Another example, let’s convert 145 decimal into binary.

\[145\div2 = 72\] remainder `1`

\[72\div2 = 36\] remainder `0`

\[36\div2 = 18\] remainder `0`

\[18\div2 = 9\] remainder `0`

\[9\div2 = 4\] remainder `1`

\[4\div2 = 2\] remainder `0`

\[2\div2 = 1\] remainder `0`

\[1\div2 = 0\] remainder `1`

The binary number representing the 145 decimal is `10010001`

.

The decimal part of the fraction is converted separately like we did above. To convert the fractional part you need to multiply it by 2.

If the integer part of the fraction is still less than `1`

, assign it a `0`

. If it’s > `1`

, then assign it a `1`

, then keep multiplying by 2 and following this scheme.

You stop when the fractional part is equal to 0.

This might never happen, and you have a periodic fraction. In this case after some point you stop. The more digits the number has, in this case, the more precision it has.

Let’s make an example. I want to convert `0.375`

to binary.

\[0.375\times2 = 0.75 \implies 0\]

\[0.75\times2 = 1.5 \implies 1\]

\[0.5\times2 = 1 \implies 1\]

You take the number `0`

or `1`

that depends on being > `1`

, and you read it from top to bottom (instead of bottom to top like we do for the integer part). The final binary that translates `.375`

is `011`

.

At this point you take the integer part (`0`

) and the fractional part (`011`

) separately, and you compose them.

The number `0.375`

converted to binary is `0.011`

© 2023 Flavio Copes made in Italy 🇮🇹 using
Notion to Site