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:Main.java

public static void main(String[] args) {

    System.out.println(Integer.numberOfLeadingZeros(10));
}

From source file:MainClass.java

public static void main(String args[]) {
    int n = 170;/*w w  w.j  a  v  a 2s. c  o m*/

    System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));

    System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));

    System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));

    System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");

}

From source file:Bits.java

public static void main(String args[]) {
    int n = 170; // 10101010 
    System.out.println("Value in binary: 10101010");

    System.out.println("Number of one bits: " + Integer.bitCount(n));

    System.out.println("Highest one bit: " + Integer.highestOneBit(n));

    System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));

    System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));

    System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));

    System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");
    n = 1;// www . j av  a  2  s.c o  m
    for (int i = 0; i < 16; i++) {
        n = Integer.rotateLeft(n, 1);
        System.out.println(n);
    }
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    int n = 170; // 10101010

    System.out.println("Value in binary: 10101010");

    System.out.println("Number of one bits: " + Integer.bitCount(n));

    System.out.println("Highest one bit: " + Integer.highestOneBit(n));

    System.out.println("Lowest one bit: " + Integer.lowestOneBit(n));

    System.out.println("Number of leading zeros : " + Integer.numberOfLeadingZeros(n));

    System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(n));

    System.out.println("\nBeginning with the value 1, " + "rotate left 16 times.");
    n = 1;/*w  w  w.  j a  va2  s .  c  o  m*/
    for (int i = 0; i < 16; i++) {
        n = Integer.rotateLeft(n, 1);
        System.out.println(n);
    }

}

From source file:Main.java

public static 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:Main.java

public static byte[] intToByteArray(final int integer, byte[] input, int offset) {
    int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? ~integer : integer)) / 8;

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

    return input;
}

From source file:Main.java

public static long readEncodedLength(InputStream input, int[] bytesReadOut) {
    try {//from w  w  w  .j a v a  2  s .  c o m
        int lbyte = input.read();
        if (lbyte == -1)
            return -1;
        if (lbyte == 0)
            throw new IllegalStateException("First length byte is 0");
        // there will always be 24 extra leading zeros from the first 3 bytes
        int nbytes = 1 + Integer.numberOfLeadingZeros(lbyte) - 24;

        // start with the first byte with the leading 1 masked out
        long value = lbyte ^ Integer.highestOneBit(lbyte);
        // read successive bytes, shifting current value each time (big-endian, most significant bytes first)
        for (int i = 1; i < nbytes; i++) {
            lbyte = input.read();
            if (lbyte == -1)
                return -1;
            value = (value << 8) | lbyte;
        }
        if (bytesReadOut != null)
            bytesReadOut[0] = nbytes;
        return value;
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:Main.java

static public int nextPowerOfTwo(int value) {
    return 1 << 32 - Integer.numberOfLeadingZeros(value - 1);
}

From source file:Main.java

/**
 * Returns the number of contiguous 0 bits starting with the most significant
 * bit of x./*ww  w.  jav a  2s  .  c o m*/
 */
static int countLeadingZeros(int x) {
    return Integer.numberOfLeadingZeros(x);
}

From source file:uk.co.modularaudio.util.math.FastMath.java

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