# Float to bits

This topic is 2614 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); 

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013614
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!