Example usage for java.lang Integer bitCount

List of usage examples for java.lang Integer bitCount

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static int bitCount(int i) 

Source Link

Document

Returns the number of one-bits 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.bitCount(10));
}

From source file:MainClass.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));
}

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;//from  w  w  w.  j  a v a2  s  .  co 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  v a 2s. c  om
    for (int i = 0; i < 16; i++) {
        n = Integer.rotateLeft(n, 1);
        System.out.println(n);
    }

}

From source file:com.xtructure.xutil.BinaryIndexedTree.java

/**
 * Creates a new {@link BinaryIndexedTree} with max index the smallest power
 * of 2 larger than the given size.//w  w w. jav  a  2 s . c o m
 * 
 * @param size the size
 * @throws IllegalArgumentException
 *             if size is not greater than 0
 */
public BinaryIndexedTree(int size) {
    validateArg("size", size, isGreaterThan(0));
    this.maxIndex = Integer.bitCount(size) == 1 ? size : Integer.highestOneBit(size) << 1;
    this.tree = new long[maxIndex + 1];
    this.freq = new long[maxIndex + 1];
}

From source file:com.offbynull.voip.kademlia.model.SimpleRouteTreeStrategy.java

/**
 * Constructs a {@link SimpleRouteTreeStrategy} object.
 * @param baseId ID of Kademlia node this supplier is generating a route tree for
 * @param branchesPerLevel number of branches to generate whenever a k-bucket splits
 * @param nodesPerBucket maximum number of nodes allowed in each k-bucket
 * @param cacheNodesPerBucket maximum number of cache nodes allowed in each k-bucket
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if any numeric argument is {@code 0} or less, or if
 * {@code branchesPerLevel < 2 || !isPowerOfTwo(branchesPerLevel)}, or if {@code baseId.getBitLength() % branchesPerLevel != 0} (if the
 * number of branches per level doesn't divide evenly in to bit length, the routing tree will have too many branches at the last level)
 *///from   w  ww.  j  a v a 2s .  c o m
public SimpleRouteTreeStrategy(Id baseId, int branchesPerLevel, int nodesPerBucket, int cacheNodesPerBucket) {
    Validate.notNull(baseId);
    Validate.isTrue(branchesPerLevel >= 2);
    Validate.isTrue(nodesPerBucket > 0);
    Validate.isTrue(cacheNodesPerBucket > 0);

    // check to make sure power of 2
    // other ways: http://javarevisited.blogspot.ca/2013/05/how-to-check-if-integer-number-is-power-of-two-example.html
    Validate.isTrue(Integer.bitCount(branchesPerLevel) == 1);

    Validate.isTrue(baseId.getBitLength() % branchesPerLevel == 0);

    this.baseId = baseId;
    this.branchesPerLevel = branchesPerLevel;
    this.nodesPerBucket = nodesPerBucket;
    this.cacheNodesPerBucket = cacheNodesPerBucket;
}

From source file:com.musicg.api.DetectionApi.java

/**
 * Determine the audio bytes contains a specific sound or not
 * /*from   ww w  .j av  a  2 s  .co  m*/
 * @param audioBytes
 *            input audio byte
 * @return true if the byes contain the specified sound
 */
public boolean isSpecificSound(byte[] audioBytes) {

    int bytesPerSample = waveHeader.getBitsPerSample() / 8;
    int numSamples = audioBytes.length / bytesPerSample;

    // numSamples required to be a power of 2
    if (numSamples > 0 && Integer.bitCount(numSamples) == 1) {
        fftSampleSize = numSamples;
        numFrequencyUnit = fftSampleSize / 2;

        // frequency could be caught within the half of nSamples according
        // to Nyquist theory
        unitFrequency = (double) waveHeader.getSampleRate() / 2 / numFrequencyUnit;

        // set boundary
        lowerBoundary = (int) (highPass / unitFrequency);
        upperBoundary = (int) (lowPass / unitFrequency);
        // end set boundary

        Wave wave = new Wave(waveHeader, audioBytes); // audio bytes of this
                                                      // frame
        short[] amplitudes = wave.getSampleAmplitudes();

        // spectrum for the clip
        Spectrogram spectrogram = wave.getSpectrogram(fftSampleSize, 0);

        double[][] spectrogramData = spectrogram.getAbsoluteSpectrogramData();

        // since fftSampleSize==numSamples, there're only one spectrum which
        // is thisFrameSpectrogramData[0]
        double[] spectrum = spectrogramData[0];

        int frequencyUnitRange = upperBoundary - lowerBoundary + 1;
        double[] rangedSpectrum = new double[frequencyUnitRange];
        System.arraycopy(spectrum, lowerBoundary, rangedSpectrum, 0, rangedSpectrum.length);

        if (frequencyUnitRange <= spectrum.length) {

            if (isPassedIntensity(spectrum)) {
                if (isPassedStandardDeviation(spectrogramData)) {
                    if (isPassedZeroCrossingRate(amplitudes)) {
                        if (isPassedFrequency(rangedSpectrum)) {
                            return true;
                        }
                    }
                }
            }

            /*
             * // run all checking for debug boolean isPassedChecking =
             * true; // rule 1: check the intensity of this frame
             * isPassedChecking &= isPassedIntensity(spectrum); // rule 2:
             * check the frequency of this frame isPassedChecking &=
             * isPassedFrequency(rangedSpectrum); // rule 3: check the zero
             * crossing rate of this frame isPassedChecking &=
             * isPassedZeroCrossingRate(amplitudes); // rule 4: check the
             * standard deviation of this frame with reference of previous
             * frames isPassedChecking &=
             * isPassedStandardDeviation(spectrogramData);
             * System.out.println("Result: " + isPassedChecking + "\n");
             * return isPassedChecking; // end run all checking for debug
             */

        } else {
            System.err.println("is error: the wave needed to be higher sample rate");
        }

    } else {
        System.out.println("The sample size must be a power of 2");
    }

    return false;
}

From source file:WorkerRunnable.java

public WorkerRunnable(int numTopics, double[][] alpha, double alphaSum, double[][] beta, double betaSum,
        int newTypes, int oldDocs, Randoms random, ArrayList<TopicAssignment> data, int[][] typeTopicCounts,
        int[] tokensPerTopic, int startDoc, int numDocs) {

    this.data = data;

    this.numTopics = numTopics;
    this.numTypes = typeTopicCounts.length;

    if (Integer.bitCount(numTopics) == 1) {
        // exact power of 2
        topicMask = numTopics - 1;//  w  w w .  j  a  v  a 2s  . c o m
        topicBits = Integer.bitCount(topicMask);
    } else {
        // otherwise add an extra bit
        topicMask = Integer.highestOneBit(numTopics) * 2 - 1;
        topicBits = Integer.bitCount(topicMask);
    }

    this.typeTopicCounts = typeTopicCounts;
    this.tokensPerTopic = tokensPerTopic;

    this.alphaSum = alphaSum;
    this.alpha = alpha;
    this.betaSum = betaSum;
    this.beta = beta;
    //DEBUG
    //System.out.println("Is beta 0? " + beta[0][0]);
    //this.betaSum = 0;
    this.betaAvg = new double[numTopics];
    int topic = 0;
    for (double[] i : beta) {
        for (double j : i) {
            this.betaAvg[topic] += j;
            //      this.betaSum += j; 
        }
        this.betaAvg[topic] /= i.length;
        topic++;
    }

    this.alphaAvg = new double[numTopics];
    for (topic = 0; topic < numTopics; topic++) {
        for (int doc = startDoc; doc < data.size() && doc < startDoc + numDocs; doc++) {
            alphaAvg[topic] += alpha[doc][topic];
        }
        this.betaAvg[topic] /= numDocs;
    }

    this.random = random;

    this.startDoc = startDoc;
    this.numDocs = numDocs;

    cachedCoefficients = new double[numTopics];
    this.topicDocCounts = new int[numTopics][numDocs];
    this.docStorageArray = new int[numTopics][numDocs];

    //System.err.println("WorkerRunnable Thread: " + numTopics + " topics, " + topicBits + " topic bits, " + 
    //               Integer.toBinaryString(topicMask) + " topic mask");

}

From source file:br.ufal.cideei.soot.instrument.bitrep.BitConfigRep.java

@Override
public int size() {
    return Integer.bitCount(id);
}

From source file:com.raphfrk.craftproxyclient.net.protocol.p172Play.P172PlayProtocol.java

@Override
public byte[] getDataArray(Packet p) {
    if (p.getId() == 0x21) {
        int primaryBitmask = ((Short) p.getField(5)) & 0xFFFF;
        int sections = Integer.bitCount(primaryBitmask);
        int maxSize = 256 + sections * 16384;

        byte[] data = (byte[]) p.getField(7);

        byte[] inflatedData = new byte[maxSize];
        int inflatedSize = CompressionManager.inflate(data, inflatedData);
        byte[] inflatedDataResized = new byte[inflatedSize];
        System.arraycopy(inflatedData, 0, inflatedDataResized, 0, inflatedSize);
        return inflatedDataResized;
    } else if (p.getId() == 0x26) {
        BulkData d = (BulkData) p.getField(2);
        int chunks = d.getChunks();
        int maxSize = chunks * 16384 * 16;
        byte[] inflatedData = new byte[maxSize];
        int inflatedSize = CompressionManager.inflate(d.getChunkData(), inflatedData);
        byte[] inflatedDataResized = new byte[inflatedSize];
        System.arraycopy(inflatedData, 0, inflatedDataResized, 0, inflatedSize);
        return inflatedDataResized;
    } else {// w w w. j a  va2  s . co m
        return null;
    }
}