Example usage for java.lang Integer numberOfLeadingZeros

List of usage examples for java.lang Integer numberOfLeadingZeros

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static int numberOfLeadingZeros(int i) 

Source Link

Document

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

Usage

From source file:piuk.blockchain.android.Hash.java

public int nLeadingZeros() {
    int n = 0;// w ww  .  j a v a  2 s.c  o m

    for (byte b : hash) {
        if (b == 0)
            n += 8;
        else {
            n += Math.max(0, Integer.numberOfLeadingZeros(b) - (3 * 8));
            break;
        }
    }

    return n;
}

From source file:org.cloudfoundry.android.cfdroid.applications.ApplicationControlFragment.java

private static int log2(int n) {
    if (n <= 0)
        throw new IllegalArgumentException();
    return 31 - Integer.numberOfLeadingZeros(n);
}

From source file:org.neo4j.io.pagecache.impl.SingleFilePageSwapper.java

private static int defaultChannelStripePower() {
    int vcores = Runtime.getRuntime().availableProcessors();
    // Find the lowest 2's exponent that can accommodate 'vcores'
    int stripePower = 32 - Integer.numberOfLeadingZeros(vcores - 1);
    return Math.min(64, Math.max(1, stripePower));
}

From source file:org.apache.sysml.runtime.util.UtilFunctions.java

public static int nextIntPow2(int in) {
    int expon = (in == 0) ? 0 : 32 - Integer.numberOfLeadingZeros(in - 1);
    long pow2 = (long) Math.pow(2, expon);
    return (int) ((pow2 > Integer.MAX_VALUE) ? Integer.MAX_VALUE : pow2);
}

From source file:com.medsphere.ovid.common.uuid.KeyGenerator.java

private byte[] intToByteArray(final int integer) {
    int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? ~integer : integer)) / 8;
    byte[] byteArray = new byte[4];

    for (int n = 0; n < byteNum; n++)
        byteArray[3 - n] = (byte) (integer >>> (n * 8));

    return (byteArray);
}

From source file:com.xsdn.main.util.Ip4Network.java

/**
 * Return an IPv4 prefix length represented by the given byte array.
 *
 * <p>/*from  w  ww.j  a v a2s . c  om*/
 *   Note that this method returns 32 if all the bits in the given array
 *   are not set.
 * </p>
 *
 * @param bytes  A byte array which represents IPv4 network mask.
 * @return  The IPv4 prefix length represented by the given byte array.
 * @throws NullPointerException
 *    {@code bytes} is {@code null}.
 * @throws IllegalArgumentException
 *    The given byte array does not represent an IPv4 network mask.
 */
public static int getPrefixLength(byte[] bytes) {
    // Verify the given network mask.
    int mask = NumberUtils.toInteger(bytes);
    if (mask == 0) {
        return Integer.SIZE;
    }

    int inv = ~mask;
    int p2 = inv + 1;
    if ((p2 & inv) != 0) {
        throw new IllegalArgumentException("Invalid IPv4 netmask: " + Integer.toHexString(mask));
    }

    return Integer.numberOfLeadingZeros(inv);
}

From source file:org.diorite.impl.world.chunk.palette.MapPaletteImpl.java

@Override
public int bitsPerBlock() {
    final int size = this.lastUsed;
    if (size <= 1) {
        return 4;
    }//from w  w w  .  j a  va 2 s. c  o m
    return Math.max(4, Integer.SIZE - Integer.numberOfLeadingZeros(size - 1));
}

From source file:com.palantir.atlasdb.ptobject.EncodingUtils.java

private static long decodeVarLong(byte[] encoded, int offset, int flipByte) {
    int first = encoded[offset] ^ flipByte;
    if (first >= 0) {
        return first;
    }/*from w w  w.  j a  v a 2s. co  m*/
    int bitsBeforeZero = Integer.numberOfLeadingZeros(~first) - 24;
    if (bitsBeforeZero == 8 && (encoded[offset + 1] ^ flipByte) < 0) {
        bitsBeforeZero++;
        if (((encoded[offset + 1] ^ flipByte) & 0x40) != 0) {
            throw new IllegalArgumentException("bad varlong, too big");
        }
    }
    int size = bitsBeforeZero + 1;

    int index = (size) / 8;
    int mask = size % 8;
    long ret = 0;
    while (index < size) {
        int b = ((encoded[offset + index] ^ flipByte) & (0xff >>> mask));
        ret <<= 8;
        ret |= b;
        mask = 0;
        index++;
    }
    return ret;
}

From source file:org.moeaframework.core.variable.BinaryIntegerVariable.java

/**
 * Returns the minimum number of bits required to represent an integer
 * within the given bounds.//  w ww .  j  a  v a  2  s.com
 * 
 * @param lowerBound the lower bound
 * @param upperBound the upper bound
 * @return the minimum number of bits required to represent an integer
 *         within the given bounds
 */
public static final int getNumberOfBits(int lowerBound, int upperBound) {
    return Integer.SIZE - Integer.numberOfLeadingZeros(upperBound - lowerBound);
}

From source file:com.palantir.atlasdb.ptobject.EncodingUtils.java

private static long decodeSignedVarLong(byte[] encoded, int offset, int flipByte) {
    boolean isNegative = ((encoded[offset] ^ flipByte) & 0x80) == 0;
    if (isNegative) {
        flipByte ^= -1;/*from www .  j ava 2s  .com*/
    }
    int first = encoded[offset] ^ flipByte;
    int bitsBeforeZero = Integer.numberOfLeadingZeros(~first) - 24;
    if (bitsBeforeZero == 8 && (encoded[offset + 1] ^ flipByte) < 0) {
        bitsBeforeZero++;
        if (((encoded[offset + 1] ^ flipByte) & 0x40) != 0) {
            bitsBeforeZero++;
            if (((encoded[offset + 1] ^ flipByte) & 0x20) != 0) {
                throw new IllegalArgumentException("bad varlong, too big");
            }
        }
    }
    int size = bitsBeforeZero;
    int index = (size) / 8;
    int mask = size % 8;
    long ret = 0;
    while (index < size) {
        int b = ((encoded[offset + index] ^ flipByte) & (0xff >>> mask));
        ret <<= 8;
        ret |= b;
        mask = 0;
        index++;
    }
    if (isNegative) {
        return ret ^ -1L;
    }
    return ret;
}