Java Power of 2 nextPowerOfTwo(int i)

Here you can find the source of nextPowerOfTwo(int i)

Description

next Power Of Two

License

Apache License

Declaration

public static int nextPowerOfTwo(int i) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static int nextPowerOfTwo(int i) {
        i -= 1;/*from  ww  w .  ja  v  a  2s.  c  o m*/
        i |= (i >> 1);
        i |= (i >> 2);
        i |= (i >> 4);
        i |= (i >> 8);
        i |= (i >> 16);

        return i + 1;
    }

    public static long nextPowerOfTwo(long i) {
        i -= 1;
        i |= (i >> 1);
        i |= (i >> 2);
        i |= (i >> 4);
        i |= (i >> 8);
        i |= (i >> 16);
        i |= (i >> 32);

        return i + 1;
    }
}

Related

  1. nextPowerOf2(int x)
  2. nextPowerOf2(int x)
  3. nextPowerOfTwo(final int targetSize)
  4. nextPowerOfTwo(final long nValue)
  5. nextPowerOfTwo(int i)
  6. nextPowerOfTwo(int i)
  7. nextPowerOfTwo(int n)
  8. nextPowerOfTwo(int num)
  9. nextPowerOfTwo(int value)