1

I want the source code for java's Float.floatToIntBits() implementation code in C\C++.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
Rajan.M
  • 11
  • 2

2 Answers2

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
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