Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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;
 }
 


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: