private static boolean get(JsArrayInteger array, int bitIndex) { // retrieve the bits for the given index int word = getWord(array, wordIndex(bitIndex)); // shift and mask the bit out return ((word >>> (bitOffset(bitIndex))) & 1) == 1; }
private static void set(JsArrayInteger array, int bitIndex) { int index = wordIndex(bitIndex); array.set(index, getWord(array, index) | (1 << (bitOffset(bitIndex)))); }
private static void clear(JsArrayInteger array, int bitIndex) { int index = wordIndex(bitIndex); int word = getWord(array, index); if (word != 0) { // mask the correct bit out setWord(array, index, word & ~(1 << (bitOffset(bitIndex)))); } }
private static void flip(JsArrayInteger array, int bitIndex) { // calculate index and offset int index = wordIndex(bitIndex); int offset = bitOffset(bitIndex); // figure out if the bit is on or off int word = getWord(array, index); if (((word >>> offset) & 1) == 1) { // if on, turn it off setWord(array, index, word & ~(1 << offset)); } else { // if off, turn it on array.set(index, word | (1 << offset)); } };
private static void set(JsArrayInteger array, int fromIndex, int toIndex) { int first = wordIndex(fromIndex); int last = wordIndex(toIndex); int startBit = bitOffset(fromIndex); int endBit = bitOffset(toIndex); if (first == last) { // set the bits in between first and last maskInWord(array, first, startBit, endBit); } else { // set the bits from fromIndex to the next 32 bit boundary if (startBit != 0) { maskInWord(array, first++, startBit, 32); } // set the bits from the last 32 bit boundary to the toIndex if (endBit != 0) { maskInWord(array, last, 0, endBit); } // // set everything in between // for (int i = first; i < last; i++) { array.set(i, 0xffffffff); } } }
public int nextSetBit(int fromIndex) { checkIndex(fromIndex); int index = wordIndex(fromIndex); // check the current word int word = getWord(array, index); if (word != 0) { for (int i = bitOffset(fromIndex); i < 32; i++) { if ((word & (1 << i)) != 0) { return (bitIndex(index)) + i; } } } index++; // find the next set word trimToSize(array); index = nextSetWord(array, index); if (index == -1) { return -1; } // return the next set bit return (bitIndex(index)) + Integer.numberOfTrailingZeros(array.get(index)); };
int rightShift = bitOffset(fromIndex); int subTo = wordIndex(toIndex + 31); JsArrayInteger subSet = slice(array, subFrom, subTo); int leftOvers = bitOffset(toIndex); if (leftOvers != 0) { maskOutWord(subSet, subTo - subFrom - 1, leftOvers, 32); int end = 32 - (bitOffset(toIndex)); int end = 32 - (bitOffset(toIndex)); current = (current << (rightShift + end)) >>> (rightShift + end); if (current != 0) {