Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { public static int nextPowerOf2(int n) { if (n <= 0 || n > (1 << 30)) { throw new IllegalArgumentException(); } n -= 1; n |= n >> 16; n |= n >> 8; n |= n >> 4; n |= n >> 2; n |= n >> 1; return n + 1; } }