Here you can find the source of floorPowerOfTwo(final int a)
Parameter | Description |
---|---|
a | A value in [1,Integer.MAX_VALUE]. |
public static int floorPowerOfTwo(final int a)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w w w . j a v a 2s.co m*/ * @param a * A value in [1,Integer.MAX_VALUE]. * @return The highest power of two <= a. */ public static int floorPowerOfTwo(final int a) { if (a <= 0) { throw new IllegalArgumentException("a [" + a + "] must be > 0"); } return Integer.highestOneBit(a); } /** * @param a * A value in [1,Long.MAX_VALUE]. * @return The highest power of two <= a. */ public static long floorPowerOfTwo(final long a) { if (a <= 0) { throw new IllegalArgumentException("a [" + a + "] must be > 0"); } // Faster than copying int method // (less computations on long). return 1L << 63 - Long.numberOfLeadingZeros(a); } }