# Float to bits

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

## Recommended Posts

Hi,
How do I convert float fraction values to bit array?
Thanks

##### Share on other sites
All data in a computer is stored as bits. So a float in C is already a bit array. Can you specify the question more precisely?

##### Share on other sites
like this click
But a better way and I dont use bitset

##### Share on other sites
Their way:
x /= 2 shifts x right by 1 ( n / 2 = n >> 1)
x % 2 gets the rightmost bit

examples:
3 = 0b11
3 % 2 = 1 (rightmost bit)
3 / 2 = 1
1 % 2 = 1 (rightmost bit)

A much easier way. (bitset constructor)
[font=verdana, arial, helvetica, sans-serif]bitset ( unsigned long val );[/font]
Having said that, what are you trying to do? Why do you need to access individual bits of a float?

##### Share on other sites

Having said that, what are you trying to do? Why do you need to access individual bits of a float?

Agreed, this is all very strange.

Ultimately, the answer is probably "memcpy in to an an array of unsigned char", but I'm not convinced the OP knows what the question is yet

##### Share on other sites
There is nothing strange there, I am using this for spatial partitioning where nodes are represented as binary fractions

##### Share on other sites
Use fixed-point math for that. Then you have floating point fraction as a nice integer.

 // 8 bits. char fraction = static_cast<char>(static_cast<size_t>(float_value * 256.0f) & 0x0ff); 

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 15
• 9
• 11
• 15