Hello,
Is there a way to get the exponent of a number without using shift and mask operators.
I can remove the mantissa using division which is equivalent to shifting,
but I am stuck at masking.
Code:
static const int32_t getExponent(uint32_t binary) {
//Shifting
printf("Exponent = %d\n", ((binary >> 23) & 255) - 127);
printf("Shift = %d\n", binary >> 23);
// binary = binary / 0b100000000000000000000000
binary = binary / 8388608;
printf("Shift by Division = %d\n", binary);
//Masking
int32_t i = 8, e = 1;
while (--i) {
e *= 2;
if (binary % 2 == 1) {
++e;
}
binary /= 2;
}
return e;
}
int main(int argc, const char *argv[]) {
const float i = 263.3;
printf("# result = %d\n", getExponent(*(uint32_t *)&i));
return 0;
}