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; }
public BitVector(int nbits) { this(); // throw an exception to be consistent // but (do we want to be consistent?) if (nbits < 0) { throw new NegativeArraySizeException("nbits < 0: " + nbits); } // even though the array's length is loosely kept to that of Sun's // "logical // length," this might help in some cases where code uses size() to fill // in // bits after constructing a BitSet, or after having one passed in as a // parameter. setLengthWords(array, wordIndex(nbits + 31)); }
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 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); } } }
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)); } };
public int nextClearBit(int fromIndex) { checkIndex(fromIndex); int index = wordIndex(fromIndex); // special case for first index int fromBit = fromIndex - (bitIndex(index)); int word = getWord(array, index); for (int i = fromBit; i < 32; i++) { if ((word & (1 << i)) == 0) { return (bitIndex(index)) + i; } } // loop through the rest while (true) { index++; word = getWord(array, index); if (word != 0xffffffff) { return (bitIndex(index)) + Integer.numberOfTrailingZeros(~word); } } }
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)); };