Example usage for java.lang Integer highestOneBit

List of usage examples for java.lang Integer highestOneBit

Introduction

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

Prototype

public static int highestOneBit(int i) 

Source Link

Document

Returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value.

Usage

From source file:Main.java

public static int PO2(int orgSize) {
    int checkSize = orgSize;
    if (checkSize > 1) {
        checkSize -= 1;//from  w w w  .j a v  a 2s.  c o m
    }

    return Integer.highestOneBit(checkSize) << 1;
}

From source file:Main.java

public static int getMapRightCapacity(int number) {
    return (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;
}

From source file:Main.java

public static long readEncodedLength(InputStream input, int[] bytesReadOut) {
    try {/*from  w ww. j a  va  2s.  co 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

public static int prevPowerOf2(final int n) {
    if (n <= 0)
        throw new IllegalArgumentException();
    return Integer.highestOneBit(n);
}

From source file:Main.java

/**
 * Returns the closest power-of-two number less than or equal to x.
 * //from w ww .  j  a  va  2s  .  co  m
 * @param x
 * @return the closest power-of-two number less then or equal to x
 */
public static int prevPow2(int x) {
    if (x < 1)
        throw new IllegalArgumentException("x must be greater or equal 1");
    return Integer.highestOneBit(x);
}

From source file:Main.java

/**
 * Returns the closest power-of-two number greater than or equal to x.
 * /*  ww w  .java2s .  co  m*/
 * @param x
 * @return the closest power-of-two number greater than or equal to x
 */
public static int nextPow2(int x) {
    if (x < 1)
        throw new IllegalArgumentException("x must be greater or equal 1");
    return isPowerOf2_unchecked(x) ? x : // x is already a power-of-two number
            Integer.highestOneBit(x) << 1;
}

From source file:Main.java

/**
 * Loads given image asset, scaling the image down if it is too big to improve performance.
 * @param context Application context/*from  w  w  w .jav  a  2  s. co m*/
 * @param path Path in the assets folder of the image to load
 * @return Loaded image bitmap
 */
public static Bitmap loadImageFromAssets(Context context, String path) {
    try {
        // Open the input stream to the image in assets
        InputStream is = context.getAssets().open(path);

        // Load the image dimensions first so that big images can be scaled down (improves memory usage)
        BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
        onlyBoundsOptions.inJustDecodeBounds = true;
        onlyBoundsOptions.inDither = true;
        BitmapFactory.decodeStream(is, null, onlyBoundsOptions);
        is.close();

        if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) {
            // There was an error while decoding
            return null;
        }

        // Find the bigger dimension (width, height)
        int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth)
                ? onlyBoundsOptions.outHeight
                : onlyBoundsOptions.outWidth;

        // Calculate the sampling ratio for images that are bigger than the thumbnail size
        double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE) : 1.0;
        int sampleSize = Integer.highestOneBit((int) Math.floor(ratio));

        // Load the image sampled using the calculated ratio
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = sampleSize;
        bitmapOptions.inDither = true;
        is = context.getAssets().open(path);
        Bitmap bitmap = BitmapFactory.decodeStream(is, null, bitmapOptions);
        is.close();

        return bitmap;
    } catch (IOException e) {
        return null;
    }
}

From source file:org.apache.hadoop.hdfs.util.ByteArrayManager.java

/**
 * @return the least power of two greater than or equal to n, i.e. return
 *         the least integer x with x >= n and x a power of two.
 *
 * @throws HadoopIllegalArgumentException
 *           if n <= 0.//from  ww  w. ja  v a  2s .co m
 */
public static int leastPowerOfTwo(final int n) {
    if (n <= 0) {
        throw new HadoopIllegalArgumentException("n = " + n + " <= 0");
    }

    final int highestOne = Integer.highestOneBit(n);
    if (highestOne == n) {
        return n; // n is a power of two.
    }
    final int roundUp = highestOne << 1;
    if (roundUp < 0) {
        final long overflow = ((long) highestOne) << 1;
        throw new ArithmeticException("Overflow: for n = " + n + ", the least power of two (the least"
                + " integer x with x >= n and x a power of two) = " + overflow + " > Integer.MAX_VALUE = "
                + Integer.MAX_VALUE);
    }
    return roundUp;
}

From source file:ro.hasna.ts.math.representation.DiscreteCosineTransform.java

/**
 * Transform a given sequence of values into Fourier coefficients using {@link FastCosineTransformer}.
 * The sequence is padded with zeros if it hasn't the right length.
 *
 * @param values the sequence of values//from   w  w w  .  j a  v a 2 s . co  m
 * @return the result of the transformation
 */
public double[] transform(double[] values) {
    // pad the input array with zeros so as to have a length == 2^k + 1
    int initialLength = values.length;
    int powerOfTwo = Integer.highestOneBit(initialLength);
    int requiredLength = powerOfTwo + 1;
    if (initialLength != requiredLength && initialLength != powerOfTwo) {
        requiredLength = (powerOfTwo << 1) + 1;
    }

    double[] copy = new double[requiredLength];
    System.arraycopy(values, 0, copy, 0, initialLength);

    // run FCT (=> DCT-I)
    double[] transform = cosineTransformer.transform(copy, TransformType.FORWARD);

    // keep only the most important coefficients
    int outputLength = (powerOfTwo >> 1) + 1;
    double[] result = new double[outputLength];
    for (int i = 0; i < outputLength && i < transform.length; i++) {
        result[i] = transform[i];
    }
    return result;
}

From source file:ro.hasna.ts.math.representation.DiscreteFourierTransform.java

/**
 * Transform a given sequence of values into Fourier coefficients using {@link FastFourierTransformer}.
 * The sequence is padded with zeros if it hasn't the right length.
 *
 * @param values the sequence of values//from   w  w  w. j av  a  2 s. c o  m
 * @return the result of the transformation
 */
public double[] transform(double[] values) {
    // pad the input array with zeros so as to have a length == 2^k
    int initialLength = values.length;
    int powerOfTwo = Integer.highestOneBit(initialLength);
    if (initialLength != powerOfTwo) {
        powerOfTwo = powerOfTwo << 1;
    }
    double[] copy = new double[powerOfTwo];
    System.arraycopy(values, 0, copy, 0, initialLength);

    // run FFT
    Complex[] complexes = fourierTransformer.transform(copy, TransformType.FORWARD);

    // keep only the most important coefficients
    int outputLength = (powerOfTwo >> 1) + 1;
    double[] result = new double[outputLength];
    double k = 2.0 / initialLength;
    result[0] = complexes[0].divide(initialLength).abs();
    for (int i = 1; i < outputLength && i < complexes.length; i++) {
        // multiply the values with 2/N
        result[i] = complexes[i].multiply(k).abs();
    }
    return result;
}