Example usage for java.lang Integer numberOfTrailingZeros

List of usage examples for java.lang Integer numberOfTrailingZeros

Introduction

In this page you can find the example usage for java.lang Integer numberOfTrailingZeros.

Prototype

@HotSpotIntrinsicCandidate
public static int numberOfTrailingZeros(int i) 

Source Link

Document

Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value.

Usage

From source file:me.zhanghai.android.patternlock.PatternView.java

/**
 * Returns the greatest common divisor of {@code a, b}. Returns {@code 0} if
 * {@code a == 0 && b == 0}./*  ww w .  j  ava  2  s  .  c o  m*/
 *
 * @throws IllegalArgumentException if {@code a < 0} or {@code b < 0}
 */
public static int gcd(int a, int b) {

    /*
     * The reason we require both arguments to be >= 0 is because otherwise, what do you return
     * on gcd(0, Integer.MIN_VALUE)? BigInteger.gcd would return positive 2^31, but positive
     * 2^31 isn't an int.
     */
    //checkNonNegative("a", a);
    if (a < 0) {
        throw new IllegalArgumentException("a (" + a + ") must be >= 0");
    }
    //checkNonNegative("b", b);
    if (b < 0) {
        throw new IllegalArgumentException("b (" + b + ") must be >= 0");
    }

    if (a == 0) {
        // 0 % b == 0, so b divides a, but the converse doesn't hold.
        // BigInteger.gcd is consistent with this decision.
        return b;
    } else if (b == 0) {
        return a; // similar logic
    }
    /*
     * Uses the binary GCD algorithm; see http://en.wikipedia.org/wiki/Binary_GCD_algorithm.
     * This is >40% faster than the Euclidean algorithm in benchmarks.
     */
    int aTwos = Integer.numberOfTrailingZeros(a);
    a >>= aTwos; // divide out all 2s
    int bTwos = Integer.numberOfTrailingZeros(b);
    b >>= bTwos; // divide out all 2s
    while (a != b) { // both a, b are odd

        // The key to the binary GCD algorithm is as follows:
        // Both a and b are odd. Assume a > b; then gcd(a - b, b) = gcd(a, b).
        // But in gcd(a - b, b), a - b is even and b is odd, so we can divide out powers of two.

        // We bend over backwards to avoid branching, adapting a technique from
        // http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax

        int delta = a - b; // can't overflow, since a and b are nonnegative

        int minDeltaOrZero = delta & (delta >> (Integer.SIZE - 1));
        // equivalent to Math.min(delta, 0)

        a = delta - minDeltaOrZero - minDeltaOrZero; // sets a to Math.abs(a - b)
        // a is now nonnegative and even

        b += minDeltaOrZero; // sets b to min(old a, b)
        a >>= Integer.numberOfTrailingZeros(a); // divide out all 2s, since 2 doesn't divide b
    }
    return a << Math.min(aTwos, bTwos);
}

From source file:com.google.uzaygezen.core.BitVectorTest.java

public void checkIntraSubHypercubeDirection(Function<Integer, BitVector> factory) {
    for (int n = 0; n < 10; ++n) {
        for (int i = 0; i < 1 << n; ++i) {
            final int expected;
            if (i == 0 | i == (1 << n) - 1) {
                expected = 0;//from w ww.ja  v  a 2 s  .c  o m
            } else {
                if ((i & 0x1) == 0) {
                    expected = Integer.numberOfTrailingZeros(i);
                    assert expected < n;
                } else {
                    expected = Integer.numberOfTrailingZeros(~i);
                    assert expected < n;
                }
            }
            BitVector bv = factory.apply(n);
            bv.copyFrom(i);
            Assert.assertEquals(expected, bv.lowestDifferentBit());
        }
    }
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private int getNetworkMaskPrefixLength(byte[] netMask) {
    ByteBuffer nm = ByteBuffer.wrap(netMask);
    int trailingZeros = Integer.numberOfTrailingZeros(nm.getInt());
    return 32 - trailingZeros;
}

From source file:org.apache.hadoop.mapred.RecoverReducerTask.java

/**
 * Return the exponent of the power of two closest to the given positive
 * value, or zero if value leq 0. This follows the observation that the msb
 * of a given value is also the closest power of two, unless the bit
 * following it is set.//  w w w  . jav  a2 s.c  o  m
 */
private static int getClosestPowerOf2(int value) {
    if (value <= 0)
        throw new IllegalArgumentException("Undefined for " + value);
    final int hob = Integer.highestOneBit(value);
    return Integer.numberOfTrailingZeros(hob) + (((hob >>> 1) & value) == 0 ? 0 : 1);
}