I want the source code for java's Float.floatToIntBits() implementation code in C\C++.
Asked
Active
Viewed 2,140 times
2 Answers
6
This seems to be the best solution:
#include <cstring>
unsigned float_to_bits(float x)
{
unsigned y;
memcpy(&y, &x, 4);
return y;
}
Of course this depends on float and unsigned consuming 4 bytes.

Community
- 1
- 1

fredoverflow
- 256,549
- 94
- 388
- 662
-
1I would use `uint32_t` instead of `unsigned`. Otherwise this is a good answer. – R.. GitHub STOP HELPING ICE Dec 20 '10 at 17:22
-
3I _think_ that this is implementing floatToRawIntBits rather than floatToIntBits – Duncan McGregor Mar 16 '11 at 10:28
1
Google Codesearch says
public static int floatToIntBits(float value) {
int result = floatToRawIntBits(value);
// Check for NaN based on values of bit fields, maximum
// exponent and nonzero significand.
if ( ((result & FloatConsts.EXP_BIT_MASK) ==
FloatConsts.EXP_BIT_MASK) &&
(result & FloatConsts.SIGNIF_BIT_MASK) != 0)
result = 0x7fc00000;
return result;
}
Follow the link for the constants.

The Archetypal Paul
- 41,321
- 20
- 104
- 134