Jump to content
  • Advertisement
Sign in to follow this  
Hiyar

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!