set_exp_zero

=Set float exponent to zero=

code format="c" /* keep a float otherwise intact, but set its exponent to zero */ float set_exp_zero(float f) { int non_exponent_mask = 0x807FFFFF; int zero_exponent = 0x3F800000; int newfloat = *(int *)&f & non_exponent mask | zero_exponent; return *(float *)&newfloat; } code

Here are all the important things to understand:
 * 1) The non-exponent mask lets us get just the part of the float other than the exponent. In binary, it's 10...01...1 with 8 zeros and 23 1s at the end.
 * 2) The zero-exponent is what a zero float exponent looks like: 01111111, but in the exponent spot (so a leading 0, and a bunch of trailing 0s). If our function was set_exp_seven, this value would be 10000110, or 0x43000000 instead of 0x3F800000.
 * 3) We get a fake int (see get_exponentfor more information), and
 * 4) & it with the non-exponent mask. Now we've unset all the exponent bits, whatever they were
 * | it with our new-exponent. Now we've set the exponent bits to exactly what we want
 * 1) Now we reverse the operation to get our fakeint to a real float, which we return.