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:com.offbynull.voip.kademlia.model.RouteTree.java

private RouteTreeNode growParent(RouteTreeNode parent, RouteTreeBranchStrategy branchStrategy,
        RouteTreeBucketStrategy bucketStrategy) {
    Validate.notNull(parent);//  ww  w . j a  va  2 s .  com
    Validate.notNull(branchStrategy);
    Validate.notNull(bucketStrategy);

    // Calculate which bucket from parent to split
    int parentNumOfBuckets = parent.getBranchCount();
    Validate.validState(Integer.bitCount(parentNumOfBuckets) == 1); // sanity check numofbuckets is pow of 2
    int parentPrefixBitLen = parent.getPrefix().getBitLength(); // num of bits in parent's prefix
    int parentSuffixBitCount = Integer.bitCount(parentNumOfBuckets - 1); // num of bits in parent's suffix
                                                                         // e.g. 8 --> 1000 - 1 = 0111, bitcount(0111) = 3

    if (parentPrefixBitLen + parentSuffixBitCount >= baseId.getBitLength()) { // should never be >, only ==, but just in case
        // The parents prefix length + the number of bits the parent used for buckets > baseId's length. As such, it isn't possible to
        // grow any further, so don't even try.
        return null;
    }

    int splitBucketIdx = (int) baseId.getBitString().getBitsAsLong(parentPrefixBitLen, parentSuffixBitCount);
    KBucket splitBucket = parent.getBranch(splitBucketIdx).getItem();
    BitString splitBucketPrefix = splitBucket.getPrefix();

    // Get number of buckets to create for new level
    int numOfBuckets = branchStrategy.getBranchCount(splitBucketPrefix);
    Validate.isTrue(numOfBuckets >= 2, "Branch count must be atleast 2, was %d", numOfBuckets);
    Validate.isTrue(Integer.bitCount(numOfBuckets) == 1, "Branch count must be power of 2");
    int suffixBitCount = Integer.bitCount(numOfBuckets - 1); // num of bits   e.g. 8 (1000) -- 1000 - 1 = 0111, bitcount(0111) = 3
    Validate.isTrue(splitBucketPrefix.getBitLength() + suffixBitCount <= baseId.getBitLength(),
            "Attempting to branch too far %s with %d bits extends past %d bits", splitBucketPrefix,
            suffixBitCount, baseId.getBitLength());

    // Split parent bucket at that branch index
    BitString newPrefix = baseId.getBitString().getBits(0, parentPrefixBitLen + suffixBitCount);
    KBucket[] newBuckets = splitBucket.split(suffixBitCount);
    for (int i = 0; i < newBuckets.length; i++) {
        BucketParameters bucketParams = bucketStrategy.getBucketParameters(newBuckets[i].getPrefix());
        int bucketSize = bucketParams.getBucketSize();
        int cacheSize = bucketParams.getCacheSize();
        newBuckets[i].resizeBucket(bucketSize);
        newBuckets[i].resizeCache(cacheSize);

        Instant lastBucketActivityTime = newBuckets[i].getLatestBucketActivityTime();
        if (lastBucketActivityTime == null) {
            lastBucketActivityTime = Instant.MIN;
        }
        bucketUpdateTimes.insert(lastBucketActivityTime, newBuckets[i].getPrefix());
    }

    // Get rid of parent bucket we just split. It branches down at that point, and any nodes that were contained within will be in the
    // newly created buckets
    bucketUpdateTimes.remove(splitBucketPrefix);

    // Create new level and set as child
    RouteTreeNode newNode = new RouteTreeNode(newPrefix, suffixBitCount, newBuckets);

    parent.setBranch(splitBucketIdx, new RouteTreeNodeBranch(newNode));

    return newNode;
}

From source file:com.callidusrobotics.locale.DungeonBuilder.java

private static int getNumRoomExits(final byte[] buffer, final int roomNumber) {
    return Integer.bitCount(getRoomExits(buffer, roomNumber));
}

From source file:com.bluepowermod.part.tube.TubeLogic.java

private boolean canPassThroughMask(TubeColor color, int colorMask) {

    return color == TubeColor.NONE || Integer.bitCount(colorMask) == 0
            || Integer.bitCount(colorMask) == 1 && (colorMask & 1 << color.ordinal()) != 0;
}

From source file:com.samsistemas.calendarview.widget.CalendarView.java

private void setTotalDayOfWeekend() {
    int[] weekendDay = new int[Integer.bitCount(mWeekend)];
    char days[] = Integer.toBinaryString(mWeekend).toCharArray();
    int day = 1;//from  ww w .  j  a v  a2s.c  om
    int index = 0;
    for (int i = days.length - 1; i >= 0; i--) {
        if (days[i] == '1') {
            weekendDay[index] = day;
            index++;
        }
        day++;
    }

    mTotalDayOfWeekend = weekendDay;
}

From source file:org.apache.drill.exec.physical.impl.join.HashJoinBatch.java

/**
 *  Call only after num partitions is known
 *///from   w  w w .j a va 2s .c o m
private void delayedSetup() {
    //
    //  Find out the estimated max batch size, etc
    //  and compute the max numPartitions possible
    //  See partitionNumTuning()
    //

    partitionMask = numPartitions - 1; // e.g. 32 --> 0x1F
    bitsInMask = Integer.bitCount(partitionMask); // e.g. 0x1F -> 5

    // Create the FIFO list of spilled partitions (pairs - inner/outer)
    spilledPartitionsList = new ArrayList<>();

    // Create array for the partitions
    partitions = new HashPartition[numPartitions];

    buildSideIsEmpty = false;
}

From source file:org.apache.drill.exec.physical.impl.common.HashTableTemplate.java

private static int roundUpToPowerOf2(int number) {
    int rounded = number >= MAXIMUM_CAPACITY ? MAXIMUM_CAPACITY
            : (rounded = Integer.highestOneBit(number)) != 0
                    ? (Integer.bitCount(number) > 1) ? rounded << 1 : rounded
                    : 1;//from  ww  w .j ava 2  s  . c  o  m

    return rounded;
}

From source file:com.github.shareme.gwsmaterialuikit.library.advancerv.draggable.RecyclerViewDragDropManager.java

private static RecyclerView.ViewHolder findSwapTargetItemForStaggeredGridLayoutManager(RecyclerView rv,
        @Nullable RecyclerView.ViewHolder draggingItem, DraggingItemInfo draggingItemInfo, int overlayItemLeft,
        int overlayItemTop, boolean vertical, boolean checkCanSwap, boolean alternative) {

    if (alternative) {
        return null;
    }//from  w  w  w  .  ja v  a 2 s  .  c om

    if (draggingItem == null) {
        return null;
    }

    RecyclerView.ViewHolder swapTargetHolder = null;

    int spanCount = CustomRecyclerViewUtils.getSpanCount(rv);
    int draggingItemSpanIndex = CustomRecyclerViewUtils.getSpanIndex(draggingItem);

    RecyclerView.ViewHolder ssvh, csvh, esvh, sevh, cevh, eevh;
    int sSpanIndex, eSpanIndex;
    int overlayItemOrigin;
    int draggingItemOrigin;

    if (vertical) {
        int sx = overlayItemLeft + 1;
        int ex = overlayItemLeft + draggingItemInfo.width - 2;
        int sy = overlayItemTop + 1;
        int cy = overlayItemTop + draggingItemInfo.height / 2 - 1;
        int ey = overlayItemTop + draggingItemInfo.height - 2;

        int sPadding = rv.getPaddingLeft();
        int ePadding = rv.getPaddingRight();
        int rvSize = rv.getWidth();
        float spanLength = (rvSize - sPadding - ePadding) * (1.0f / spanCount);

        sSpanIndex = Math.min(Math.max((int) ((sx - draggingItemInfo.margins.left - sPadding) / spanLength), 0),
                spanCount - 1);
        eSpanIndex = Math.min(
                Math.max((int) ((ex - draggingItemInfo.margins.right - sPadding) / spanLength), 0),
                spanCount - 1);

        overlayItemOrigin = overlayItemTop;
        draggingItemOrigin = draggingItem.itemView.getTop();

        ssvh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, sx, sy);
        csvh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, sx, cy);
        esvh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, sx, ey);
        sevh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, ex, sy);
        cevh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, ex, cy);
        eevh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, ex, ey);
    } else {
        int sx = overlayItemLeft + 1;
        int cx = overlayItemLeft + draggingItemInfo.width / 2 - 1;
        int ex = overlayItemLeft + draggingItemInfo.width - 2;
        int sy = overlayItemTop + 1;
        int ey = overlayItemTop + draggingItemInfo.height - 2;

        int sPadding = rv.getPaddingTop();
        int ePadding = rv.getPaddingBottom();
        int rvSize = rv.getHeight();
        float spanLength = (rvSize - sPadding - ePadding) * (1.0f / spanCount);

        sSpanIndex = Math.min(Math.max((int) ((sx - draggingItemInfo.margins.top - sPadding) / spanLength), 0),
                spanCount - 1);
        eSpanIndex = Math.min(Math.max((int) ((ex - draggingItemInfo.margins.left - sPadding) / spanLength), 0),
                spanCount - 1);

        overlayItemOrigin = overlayItemLeft;
        draggingItemOrigin = draggingItem.itemView.getLeft();

        ssvh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, sx, sy);
        csvh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, cx, sy);
        esvh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, ex, sy);
        sevh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, sx, ey);
        cevh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, cx, ey);
        eevh = CustomRecyclerViewUtils.findChildViewHolderUnderWithoutTranslation(rv, ex, ey);
    }

    int sState = 0;
    int eState = 0;

    if (csvh != null) {
        sState |= 1;
        if (csvh == ssvh) {
            sState |= 2;
        }
        if (csvh == esvh) {
            sState |= 4;
        }
    }

    if (cevh != null) {
        eState |= 1;
        if (cevh == sevh) {
            eState |= 2;
        }
        if (cevh == eevh) {
            eState |= 4;
        }
    }

    int sCount = Integer.bitCount(sState);
    int eCount = Integer.bitCount(eState);

    if (sSpanIndex != draggingItemSpanIndex && sSpanIndex == eSpanIndex) {
        if (sCount == 3) {
            swapTargetHolder = csvh;
        } else if (eCount == 3) {
            swapTargetHolder = cevh;
        }
    }

    if (swapTargetHolder == null) {
        if (sCount == 2 && eCount != 2) {
            swapTargetHolder = csvh;
        } else if (eCount == 2 && sCount != 2) {
            swapTargetHolder = cevh;
        }
    }

    if (swapTargetHolder != null) {
        int swapTargetItemSpanIndex = CustomRecyclerViewUtils.getSpanIndex(swapTargetHolder);

        if (draggingItemSpanIndex == swapTargetItemSpanIndex) {
            if (overlayItemOrigin <= draggingItemOrigin) {
                // upward or left
                if (((sState | eState) & 2) != 0) {
                    swapTargetHolder = null;
                }
            } else {
                // downward or right
                if (((sState | eState) & 4) != 0) {
                    swapTargetHolder = null;
                }
            }
        }
    }

    return swapTargetHolder;
}