Java examples for java.lang:Math Calculation
ensure Power Of 2
//package com.java2s; public class Main { public static final int MAXIMUM_POW_OF_2 = 1 << 30; public static int ensurePowerOf2(int x) { int n = x - 1; n |= n >>> 1;//from w w w .ja v a2s .co m n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; return (n < 0) ? 1 : (n >= MAXIMUM_POW_OF_2) ? MAXIMUM_POW_OF_2 : n + 1; } }