42

Can anyone please tell me how can I convert this float number: 12.25 to binary? I know how to convert the "12" but not the 0.25

Any help is much appreciated. Thanks

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Slim Black
  • 423
  • 1
  • 4
  • 4
  • 4
    Manually :) I can do it programatically after that. – Slim Black Oct 17 '10 at 18:06
  • Yes :) I need to know how to calculate the 0.25 to binary – Slim Black Oct 17 '10 at 18:13
  • What's your algorithm for the "12"? I think the same algorithm would work just the same for the ".25", with perhaps only the change of "2" to "1/2". – Ken Oct 17 '10 at 18:14
  • For the 12 I just keep on dividing it by 2 and get the remainders. – Slim Black Oct 17 '10 at 18:17
  • Check this out: [http://kipirvine.com/asm/workbook/floating_tut.htm](http://kipirvine.com/asm/workbook/floating_tut.htm) – PSS Jan 29 '11 at 20:12
  • A deleted post below linked to a blog that has great intuition about how floating point numbers are stored on disk, and should be helpful. https://blog.penjee.com/binary-numbers-floating-point-conversion – Eric Leschinski Oct 25 '17 at 11:42

6 Answers6

35

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.

Community
  • 1
  • 1
Govind Prabhu
  • 483
  • 4
  • 6
30

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

kren470
  • 335
  • 1
  • 3
  • 13
Abhi
  • 432
  • 4
  • 4
  • 13
    @Slim Black: Beware: that works fine for numbers like 0.25, which have exact representations in binary, but not for numbers like 0.1, which don't: 0.1*2 = 0.2, 0.2*2 = 0.4, 0.4*2 = 0.8, 0.8*2 = 1.6, 0.6*2 = 1.2, 0.2*2 = 0.4, ... It repeats forever, and the result is 0.0(0011) (the part in parentheses repeats). – Rick Regan Oct 17 '10 at 21:42
  • 1
    @Slim Black: And note, to implement this correctly programmatically, you'll need decimal arithmetic -- see my article http://www.exploringbinary.com/base-conversion-in-php-using-bcmath/, specifically section dec2bin_f() .) – Rick Regan Oct 17 '10 at 21:50
14

(d means decimal, b means binary)

  1. 12.25d is your float.
  2. You write 12d in binary and remove it from your float. Only the remainder (.25d) will be left.
  3. You write the dot.
  4. 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).
comonad
  • 5,134
  • 2
  • 33
  • 31
  • 1
    What if my float is 5.1? Doing yours steps, I got into a infinite loop, please help! – nautilusvn Aug 12 '14 at 07:02
  • 2
    @nautilusvn: because there is no power of 2 that is also a multiple of 10, that number has an infinite sequence of digits. you might want to abort your computation somewhere. – comonad Aug 20 '14 at 11:48
  • 1
    @nautilusvn hm, yes, "while not zero" is a silly statement.. changing that. – comonad Aug 08 '16 at 19:45
6

The float value is stored in IEEE 754 format so we can't convert it directly like integer, char to binary.

But we can convert float to binary through a pointer.

#include <stdio.h>

int main()
{
    float a = 7.5;
    int i;
    int * p;

    p = &a;
    for (i = sizeof(int) * 8 - 1; i >= 0; i--)
    {   
        printf("%d", (*p) >> i & 1); 
    }   

    return 0;
}

Output

0 10000001 11100000000000000000000

Spaces added for clarification, they are not included as part of the program.

carpinchosaurio
  • 1,175
  • 21
  • 44
ilango victor
  • 61
  • 1
  • 2
1
x = float(raw_input("enter number between 0 and 1: "))

p = 0
while ((2**p)*x) %1 != 0:
    p += 1
    # print p

    num = int (x * (2 ** p))
    # print num

    result = ''
    if num == 0:
        result = '0'
    while num > 0:
        result = str(num%2) + result
        num = num / 2

    for i in range (p - len(result)):
        result = '0' + result
    result = result[0:-p] + '.' + result[-p:]

print result #this will print result for the decimal portion
iLabQC
  • 41
  • 2
  • maybe you also want to add the reference to where you got that from. this python code looks familiar to me. – Kin Cheung Jan 18 '16 at 04:49
0
void transfer(double x) {
  unsigned long long * p = (unsigned long long * ) & x;
  for (int i = sizeof(unsigned long long) * 8 - 1; i >= 0; i--) {
    cout << (( * p) >> i & 1);
  }
}
Mohit Kushwaha
  • 1,003
  • 1
  • 10
  • 15