Java floor floorPowerOf2(final int n)

Here you can find the source of floorPowerOf2(final int n)

Description

Computes the floor power of 2 within the range [1, 2^30].

License

Apache License

Parameter

Parameter Description
n The given argument.

Return

the floor power of 2.

Declaration

public static int floorPowerOf2(final int n) 

Method Source Code

//package com.java2s;
/*//from w w  w.j  a v  a 2s  . c om
 * Copyright 2015-16, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */

public class Main {
    /**
     * Computes the floor power of 2 within the range [1, 2^30]. This is the largest positive power of
     * 2 that equal to or less than the given n. <br>
     * For:
     * <ul>
     * <li>n &le; 1: returns 1</li>
     * <li>2^30 &le; n &le; 2^31 -1 : returns 2^30</li>
     * <li>n == a power of 2 : returns n</li>
     * <li>otherwise returns the largest power of 2 less than n</li>
     * </ul>
     *
     * @param n The given argument.
     * @return the floor power of 2.
     */
    public static int floorPowerOf2(final int n) {
        if (n <= 1) {
            return 1;
        }
        return Integer.highestOneBit(n);
    }
}

Related

  1. floorPlaneAndHorizToPlanView(final int[][] frameCells, final short frame[], final int h)
  2. floorPlaneToPlanView(int[][] frameCells, final int h)
  3. floorPositive(float value)
  4. floorPot(float value)
  5. floorPOT(int n)
  6. floorPowerOf2(final int x)
  7. floorPowerOf2(int n)
  8. floorPowerOfTwo(final int a)
  9. floorSec(long milli)