I have a function of the following table.
| input: e | output: 1 << e |
|---|---|
0 |
1 |
1 |
2 |
| ... | ... |
31 |
-2147483648 |
static int value(int exponent) {
return 1 << exponent;
}
Now, how can I get the exponent from a value?
I tried the following code and it fails for the last entry of -2147483648.
int exponent(int value) {
return (int) (Math.log(value) / Math.log(2)); // -> NaN / Math.log(2)
}
And I managed to fix it like this.
int exponent(int value) {
if (value == Integer.MIN_VALUE) {
return (int) (Math.log(value - 1) / Math.log(2)) + 1;
}
return (int) (Math.log(value) / Math.log(2));
}
Is there any better/proper way to do this?
You can use
Integer.numberOfLeadingZerosfor that. According to its JavaDoc:To implement your
exponentmethod, simply copy what the JavaDoc suggests:Note that this also works for negative numbers in your case, even though the logarithm is not defined for non-positive numbers: For all negative numbers, the left-most bit will be 1 due to their representation in the two's complement. Therefore, for any negative value,
Integer.numberOfLeadingZerosreturns 0, and hence yourexponentmethod calculates31.Also note, that if
value == 0, thenexponentreturns-1which is a case you might want to take care of. For example, you could throw anIllegalArgumentExceptionsince the logarithm is undefined for0.