Hi,
How do I convert float fraction values to bit array?
Thanks
Float to bits
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?
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?
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?
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
There is nothing strange there, I am using this for spatial partitioning where nodes are represented as binary fractions
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);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement