Skip to main content
gamedev.net
🔒 Locked

Float to bits

Started by Hiyar Oct 19, 2011 at 10:05 PM 5 replies 1k views
Original Post
Hiyar
Hiyar
Hi,
How do I convert float fraction values to bit array?
Thanks
Hiyar
Hiyar
like this click
But a better way and I dont use bitset
jameszhao00
jameszhao00
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?
the_edd
the_edd

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
Hiyar
Hiyar
There is nothing strange there, I am using this for spatial partitioning where nodes are represented as binary fractions
Codarki
Codarki
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);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.