2 ms·
You could use that approach in Java... public int roundUpToPowerOfTwo(int i) { return 1 << (int)(Math.ceil(Math.log(i)/Math.log(2))); } ...but the first a
by gojomo 19y ago
You could use that approach in Java...
public int roundUpToPowerOfTwo(int i) {
return 1 << (int)(Math.ceil(Math.log(i)/Math.log(2)));
}
...but the first approach that occurred to this Java coder was instead...
public int roundUpToPowerOfTwo(int i) {
int rounded = 1;
while(rounded<i) rounded <<= 1;
return rounded;
}