I want to find the power of 2 that any integer contains. Like 12 = 2*2*3 so answer should come as 2, 28 = 2*2*7 so answer should come as 2 and so on.
int powerOf2InNumber = (int)Math.floor(Math.log(number) / Math.log(2));
I tried the above code but in cases like 28,26,10 etc i'm getting wrong answer.
There is a handy built-in function,
int powersOf2 = Integer.numberOfTrailingZeros(number);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With