Calculate next Power Of 2 via bit operation - Android java.lang

Android examples for java.lang:Integer

Description

Calculate next Power Of 2 via bit operation

Demo Code

public class Main {
  public static int nextPowerOf2(int n) {
    n -= 1;//w  w w .j  a  va 2  s.  co m
    n |= n >>> 16;
    n |= n >>> 8;
    n |= n >>> 4;
    n |= n >>> 2;
    n |= n >>> 1;
    return n + 1;
  }
}

Related Tutorials