How to find the largest power of 2 less than the given number
- by nazar_art
I need to find the largest power of 2 less than the given number.
And I stuck and can't find any solution.
Code:
public class MathPow
{
public int largestPowerOf2 (int n)
{
int res = 2;
while (res < n) {
res =(int)Math.pow(res, 2);
}
return res;
}
}
This doesn't work correctly.
Testing output:
Arguments Actual Expected
-------------------------
9 16 8
100 256 64
1000 65536 512
64 256 32
How to solve this issue?