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:com.github.jonmarsh.waveform_processing_for_imagej.WaveformUtils.java

/**
 * Computes the number required to add to the input to yield the next
 * highest positive integral power of 2. If input {@code n} is negative,
 * output is {@code -n}./*from  w w  w.j a v  a  2s  .co  m*/
 *
 * @param n input integer
 * @return   number required to add to the input integer to yield the next
 *         highest positive integral power of 2
 */
public static final int amountToPadToNextPowerOf2(int n) {
    int highestOneBit = Integer.highestOneBit(n);
    return (n - highestOneBit == 0 ? 0 : highestOneBit * 2 - n);
}

From source file:org.apache.fontbox.ttf.TTFSubFont.java

private byte[] buildCmapTable() throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    LOG.debug("Building table [cmap]...");
    /*/* ww w  .j ava  2 s.  co  m*/
     * UInt16    version    Version number (Set to zero)
     * UInt16    numberSubtables    Number of encoding subtables
     */
    writeUint16(dos, 0);
    writeUint16(dos, 1);
    /*
     * UInt16    platformID    Platform identifier
     * UInt16    platformSpecificID    Platform-specific encoding identifier
     * UInt32    offset    Offset of the mapping table
     */
    writeUint16(dos, 3); // unicode
    writeUint16(dos, 1); // Default Semantics
    writeUint32(dos, 4 * 2 + 4);
    // mapping of type 4.
    Iterator<Entry<Integer, Integer>> it = this.characters.entrySet().iterator();
    it.next();
    Entry<Integer, Integer> lastChar = it.next();
    Entry<Integer, Integer> prevChar = lastChar;
    int lastGid = this.getNewGlyphId(lastChar.getValue());

    int[] startCode = new int[this.characters.size()];
    int[] endCode = new int[this.characters.size()];
    int[] idDelta = new int[this.characters.size()];
    int nseg = 0;
    while (it.hasNext()) {
        Entry<Integer, Integer> curChar = it.next();
        int curGid = this.getNewGlyphId(curChar.getValue());

        if (curChar.getKey() != prevChar.getKey() + 1
                || curGid - lastGid != curChar.getKey() - lastChar.getKey()) {
            // Don't emit ranges, which map to the undef glyph, the
            // undef glyph is emitted a the very last segment.
            if (lastGid != 0) {
                startCode[nseg] = lastChar.getKey();
                endCode[nseg] = prevChar.getKey();
                idDelta[nseg] = lastGid - lastChar.getKey();
                ++nseg;
            }
            // shorten ranges which start with undef by one.
            else if (!lastChar.getKey().equals(prevChar.getKey())) {
                startCode[nseg] = lastChar.getKey() + 1;
                endCode[nseg] = prevChar.getKey();
                idDelta[nseg] = lastGid - lastChar.getKey();
                ++nseg;
            }
            lastGid = curGid;
            lastChar = curChar;
        }
        prevChar = curChar;
    }
    // trailing segment
    startCode[nseg] = lastChar.getKey();
    endCode[nseg] = prevChar.getKey();
    idDelta[nseg] = lastGid - lastChar.getKey();
    ++nseg;
    // notdef character.
    startCode[nseg] = 0xffff;
    endCode[nseg] = 0xffff;
    idDelta[nseg] = 1;
    ++nseg;

    /*
     * UInt16    format    Format number is set to 4     
     * UInt16    length    Length of subtable in bytes     
     * UInt16    language    Language code for this encoding subtable, or zero if language-independent     
     * UInt16    segCountX2    2 * segCount     
     * UInt16    searchRange    2 * (2**FLOOR(log2(segCount)))     
     * UInt16    entrySelector    log2(searchRange/2)     
     * UInt16    rangeShift    (2 * segCount) - searchRange     
     * UInt16    endCode[segCount]    Ending character code for each segment, last = 0xFFFF.    
     * UInt16    reservedPad    This value should be zero    
     * UInt16    startCode[segCount]    Starting character code for each segment    
     * UInt16    idDelta[segCount]    Delta for all character codes in segment     
     * UInt16    idRangeOffset[segCount]    Offset in bytes to glyph indexArray, or 0     
     * UInt16    glyphIndexArray[variable]    Glyph index array
     */

    writeUint16(dos, 4);
    writeUint16(dos, 8 * 2 + nseg * (4 * 2));
    writeUint16(dos, 0);
    writeUint16(dos, nseg * 2);
    int nsegHigh = Integer.highestOneBit(nseg);
    writeUint16(dos, nsegHigh * 2);
    writeUint16(dos, log2i(nsegHigh));
    writeUint16(dos, 2 * (nseg - nsegHigh));

    for (int i = 0; i < nseg; ++i) {
        writeUint16(dos, endCode[i]);
    }
    writeUint16(dos, 0);
    for (int i = 0; i < nseg; ++i) {
        writeUint16(dos, startCode[i]);
    }
    for (int i = 0; i < nseg; ++i) {
        writeUint16(dos, idDelta[i]);
    }
    for (int i = 0; i < nseg; ++i) {
        writeUint16(dos, 0);
    }
    LOG.debug("Finished table [cmap].");
    return bos.toByteArray();
}

From source file:org.tvheadend.tvhclient.htsp.HTSService.java

public void cacheImage(String url, File f) throws MalformedURLException, IOException {
    InputStream is;//from w ww . ja v  a  2s.co  m

    if (url.startsWith("http")) {
        is = new BufferedInputStream(new URL(url).openStream());
    } else if (connection.getProtocolVersion() > 9) {
        is = new HTSFileInputStream(connection, url);
    } else {
        Log.d(TAG, "Unhandled url: " + url);
        return;
    }

    OutputStream os = new FileOutputStream(f);

    float scale = getResources().getDisplayMetrics().density;
    int width = (int) (64 * scale);
    int height = (int) (64 * scale);

    // Set the options for a bitmap and decode an input stream into a bitmap
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, o);
    is.close();

    if (url.startsWith("http")) {
        is = new BufferedInputStream(new URL(url).openStream());
    } else if (connection.getProtocolVersion() > 9) {
        is = new HTSFileInputStream(connection, url);
    }

    // Set the sample size of the image. This is the number of pixels in
    // either dimension that correspond to a single pixel in the decoded
    // bitmap. For example, inSampleSize == 4 returns an image that is 1/4
    // the width/height of the original, and 1/16 the number of pixels.
    int ratio = Math.max(o.outWidth / width, o.outHeight / height);
    int sampleSize = Integer.highestOneBit((int) Math.floor(ratio));
    o = new BitmapFactory.Options();
    o.inSampleSize = sampleSize;

    // Now decode an input stream into a bitmap and compress it.
    Bitmap bitmap = BitmapFactory.decodeStream(is, null, o);
    if (bitmap != null) {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    }
    os.close();
    is.close();
}

From source file:org.apache.hadoop.hive.ql.exec.persistence.BytesBytesMultiHashMap.java

private static int nextHighestPowerOfTwo(int v) {
    return Integer.highestOneBit(v) << 1;
}

From source file:org.apache.hadoop.hive.ql.exec.persistence.BytesBytesMultiHashMap.java

private static int nextLowestPowerOfTwo(int v) {
    return Integer.highestOneBit(v);
}

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  .ja  v a  2 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);
}