Reputation: 445
I have some natural big number in double. I need to get 30 rightmost bits of it (of integral part). If it was integer the operation was:
var & 0x3FFFFFFF
I can implement some functions for the purpose, but I need some simple solution. Is there one?
*edit:
All the answers doesn't work for me. I'll try to explain: for example I have double x = 9362446620820194.0000, 30 rightmost bits of integral part of that number is the number 957350626.
I could use uint64_t instead of double, but I need support for 32 bit systems.
p.s. I mean simple binary number representation, not the machine (memory)
Upvotes: 3
Views: 1045
Reputation: 363767
Assuming 8-bit unsigned char
:
unsigned char const *p = (unsigned char const *)&var;
long rightmost30 = (p[sizeof(double) - 4] & 0x3F) << 24
| p[sizeof(double) - 3] << 16
| p[sizeof(double) - 2] << 8
| p[sizeof(double) - 1];
Though really, you should have a look at frexp
and related functions.
Upvotes: 2
Reputation:
The casting hack:
float var = 0.2987647892;
uint32_t bit_repr = *(uint32_t *)&var;
uint32_t masked = bit_repr & 0x3FFFFFFF;
Upvotes: 0