The method below multiplies a float by 2 by manipulating its bits to increment the exponent.
See the javadoc for Float.intBitsToFloat() for details on the IEEE754 floating-point representation. Unlike the previous blog entry, the methods used here have been around a long time.
static float multiplyByTwo(float x) {
// View the float as 32 bits
int bits = Float.floatToIntBits(x);
// Extract the bits that represent the exponent
int exponent = (bits >> 23) & 0xFF;
// clear the exponent bits
bits &= ~(0xFF << 23);
// Increment the exponent and set this new value
bits |= ((exponent+1) & 0xFF) << 23;
// Return the bits as a float
return Float.intBitsToFloat(bits);
}



