How to convert float number to Binary?
Consider below example
Convert 2.625 to binary.
We will consider the integer and fractional part separately.
The integral part is easy, 2 = 10.
For the fractional part:
0.625 × 2 = 1.25 1 Generate 1 and continue with the rest.
0.25 × 2 = 0.5 0 Generate 0 and continue.
0.5 × 2 = 1.0 1 Generate 1 and nothing remains.
So 0.625 = 0.101, and 2.625 = 10.101.
See this link for more information.
Keep multiplying the number after decimal by 2 till it becomes 1.0:
0.25*2 = 0.50
0.50*2 = 1.00
and the result is in reverse order being .01
(d means decimal, b means binary)
- 12.25d is your float.
- You write 12d in binary and remove it from your float. Only the remainder (.25d) will be left.
- You write the dot.
- While the remainder (0.25d) is not zero (and/or you want more digits), multiply it with 2 (-> 0.50d), remove and write the digit left of the dot (0), and continue with the new remainder (.50d).