#### Archived

This topic is now archived and is closed to further replies.

# representing a float value as its bit representation

This topic is 5646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am looking for a C/C++ function that will allow me to input a floating point variable and output the ''1'' or ''0'' at that point in memory. I''ve tried all that I know (my programming experience isn''t too extensive), but I can do it well enough with an unsigned int value (sample code for that is all over the place...) Thanks

##### Share on other sites
The binary representation for a 32 bit floating point is a little more complicated. I can point you in the right direction:

Bit 1: The Sign. 0 is positive.
Bits 2-10: The mantissa (ie: 2^x where these bits represent the x. Note that you subtract 127 from the raw value of these bits to get the proper mantissa and to fidn the binary form that should go here you add 127 to the mantissa; this is done to help speed up sorting and is called bias 127 representation)
Bits 9-32 - The fractional part of the float. Note that you never store the first 1 of the scientific notation because it is ALWAYS there. This is called the hidden bit trick.

For example the computer stores 136.78 as 1.3678*10^2
Note that in this form there is always a 1 before the decimal point. This 1 is not stored in the 32 bits because it is always assumed to be there. So bits 9-32 store the .3678 part. Also note that binary numbers to the right of the decimal point work as in base 10 but for base 2. ie the first place is 1/2 the second place is 1/4 the third is 1/8 and so on. Does this make sense?

[edited by - acw83 on January 29, 2003 11:43:04 PM]

[edited by - acw83 on January 29, 2003 11:54:15 PM]

##### Share on other sites
I figure I''ll give you an example:

17.125

Bit 1: Positive so set to 0
Now convert the number to binary form:
17 = 10001
.125 = .001
So the binary form of 17.125 is 10001.001
Now set to scientific notation:
1.0001001 * 2^4 (Base 2 notation...)
Ok so now the mantissa is 4. So set bits 2-9 to 4 + 127:
Bits 2-10: 10000011
Now bits 11-32 Get the .0001001 (Remember not to include the 1 before the binary point)
So final is:
0100000110001001 and then you add enough 0''s to make 32 total bits:
01000001100010010000000000000000

Does this help?

##### Share on other sites
well this works for outputting the stored representation of a float in hexadecimal, then it''s easy enough to convert hex to binary

float x = 16.6667;
cout << hex << (long & x;

##### Share on other sites
That helps a lot, I am ok on how the floating point representation works- I was just having trouble looking at it, since the same technique couldn''t be used, but I think I have it cleared up now, thanks a bunch.

##### Share on other sites
float x=1.56;
long *ToDisp;

ToDisp = (long*)&x;

Display the binary of the long, to view the binary of the float . This should work properly, although I haven''t tested it myself.

##### Share on other sites
quote:
Original post by Beserkerfork
I am looking for a C/C++ function that will allow me to input a floating point variable and output the ''1'' or ''0'' at that point in memory. I''ve tried all that I know (my programming experience isn''t too extensive), but I can do it well enough with an unsigned int value (sample code for that is all over the place...)

It depends exactly on what you are trying to achieve. If you truly want to work with the bits of the float, then you can reinterpret_cast it to an integral type:
unsigned int &ui = reinterpret_cast(f);

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001782
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!