DEV Community

csm-yujinkim
csm-yujinkim

Posted on

Convert a decimal fraction to binary

In this article, we deal with item 5 of the following key skills that build up to understanding the floating point system:

  1. Define and identify the most and least significant bits in a word. Count how many combinations are possible in a word given its bit length.
  2. Encode a positive integer in binary. Decode an unsigned integer to decimal.
  3. Encode an integer in two's complement notation. Decode a signed integer to decimal.
  4. Use sign extension to expand the number of bits in a word. Explain the mathematics.
  5. Convert a decimal fraction to binary. Explain why this does not require any particular encoding scheme, such as a floating point standard.

Expanding our idea

We use the same method as we used before to convert number representations.

Practice

QUESTION 1 Convert the decimal number 25.5 to unsigned binary.

Answer: 25.5 - 16 = 9.5

9.5 - 8 = 1.5

1.5 - 1 = 0.5

0.5 - 0.5 = 0

The powers of two used are: the 4th, 3rd, 0th, and negative 1st. Negative powers of two gets placed to the right of the binary point. Arrange: 0001 1001.1.

QUESTION 2 Convert the decimal number 16.1 to unsigned binary.

Answer: 16.1 - 16 = 0.1

0.1 - 0.0625 [ or 2^-4 ] = 0.0375

0.0375 - 0.0315 [ or 2^-5 ] = 0.00625

0.00625 - 0.00390625 [ or 2^-8 ] = 0.00234375‬

0.00234375‬ - 0.001953125 [ or 2^-9 ] = 0.000390625‬

0.000390625‬ - 0.000244140625‬ [ or 2^-12 ] = 0.000146484375‬

...

We see that the pattern .0001 1001 1001 ... is repeating. Arrange the bits: 0001 0000.0001 1001 1001 ....

QUESTION 3 Convert 1011.0101 to decimal.

The bits below the binary point (.) have the values of the negative powers of two.

The answer is 2^3 + 2^1 + 2^0 + 2^-2 + 2^-4 = 10.3125‬.

It's not that difficult!

Top comments (0)