Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating Point to Binary Value(C++)

Tags:

I want to take a floating point number in C++, like 2.25125, and a int array filled with the binary value that is used to store the float in memory (IEEE 754).

So I could take a number, and end up with a int num[16] array with the binary value of the float: num[0] would be 1 num[1] would be 1 num[2] would be 0 num[3] would be 1 and so on...

Putting an int into an array isn't difficult, just the process of getting the binary value of a float is where I'm stuck. Can you just read the binary in the memory that the float variable? If not, how could I go about doing this in C++?

EDIT: The reason for doing the comparison this way is that I am wanting to learn to do bitwise operations in C++.

like image 938
user58389 Avatar asked Jan 23 '09 18:01

user58389


People also ask

How do you convert a floating point decimal to binary?

To convert a decimal number to binary floating point representation: Convert the absolute value of the decimal number to a binary integer plus a binary fraction. Normalize the number in binary scientific notation to obtain m and e. Set s=0 for a positive number and s=1 for a negative number.

Can you convert a float into binary?

Fractional part (0.25) To convert the fractional part to binary, multiply fractional part with 2 and take the one bit which appears before the decimal point. Follow the same procedure with after the decimal point (.) part until it becomes 1.0.

How are floats represented in binary in C?

Floating point basics etc. The mantissa is usually represented in base b, as a binary fraction. So (in a very low-precision format), 1 would be 1.000*20, 2 would be 1.000*21, and 0.375 would be 1.100*2-2, where the first 1 after the decimal point counts as 1/2, the second as 1/4, etc.


1 Answers

Use union and bitset:

#include <iostream>
#include <bitset>
#include <climits>

int main()
{
    union
    {
        float input; // assumes sizeof(float) == sizeof(int)
        int   output;
    } data;

    data.input = 2.25125;

    std::bitset<sizeof(float) * CHAR_BIT> bits(data.output);
    std::cout << bits << std::endl;

    // or
    std::cout << "BIT 4: " << bits[4] << std::endl;
    std::cout << "BIT 7: " << bits[7] << std::endl;
}

It may not be an array but you can access bits with [] operator as if you were using an array.

Output

$ ./bits
01000000000100000001010001111011
BIT 4: 1
BIT 7: 0
like image 156
Martin York Avatar answered Sep 28 '22 21:09

Martin York