Java floor floorPowerOfTwo(final int a)

Here you can find the source of floorPowerOfTwo(final int a)

Description

floor Power Of Two

License

Open Source License

Parameter

Parameter Description
a A value in [1,Integer.MAX_VALUE].

Return

The highest power of two <= a.

Declaration

public static int floorPowerOfTwo(final int a) 

Method Source Code

//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);
    }
}

Related

  1. floorPot(float value)
  2. floorPOT(int n)
  3. floorPowerOf2(final int n)
  4. floorPowerOf2(final int x)
  5. floorPowerOf2(int n)
  6. floorSec(long milli)
  7. floorSec(long valueMs)
  8. floorSqrt(long i)
  9. floorToLong(float pX)