/** * Returns the bit at index {@code index}. Indexes greater than the current length return false. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public boolean get(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= longCount) { return false; } return (bits[arrayIndex] & (1L << index)) != 0; }
/** * Returns the index of the first bit that is set on or before {@code index}, or -1 if * no lower bits are set or {@code index == -1}. * @throws IndexOutOfBoundsException if {@code index < -1}. * @since 1.7 */ public int previousSetBit(int index) { if (index == -1) { return -1; } checkIndex(index); // TODO: optimize this. for (int i = index; i >= 0; --i) { if (get(i)) { return i; } } return -1; }
/** * Returns the index of the first bit that is clear on or before {@code index}, or -1 if * no lower bits are clear or {@code index == -1}. * @throws IndexOutOfBoundsException if {@code index < -1}. * @since 1.7 */ public int previousClearBit(int index) { if (index == -1) { return -1; } checkIndex(index); // TODO: optimize this. for (int i = index; i >= 0; --i) { if (!get(i)) { return i; } } return -1; }
/** * Returns the index of the first bit that is clear on or after {@code index}. * Since all bits past the end are implicitly clear, this never returns -1. * @throws IndexOutOfBoundsException if {@code index < 0}. */ public int nextClearBit(int index) { checkIndex(index); int arrayIndex = index / 64; if (arrayIndex >= longCount) { return index; } long mask = ALL_ONES << index; if ((~bits[arrayIndex] & mask) != 0) { return 64 * arrayIndex + Long.numberOfTrailingZeros(~bits[arrayIndex] & mask); } while (++arrayIndex < longCount && bits[arrayIndex] == ALL_ONES) { } if (arrayIndex == longCount) { return 64 * longCount; } return 64 * arrayIndex + Long.numberOfTrailingZeros(~bits[arrayIndex]); }
/** * Returns the index of the first bit that is set on or after {@code index}, or -1 * if no higher bits are set. * @throws IndexOutOfBoundsException if {@code index < 0}. */ public int nextSetBit(int index) { checkIndex(index); int arrayIndex = index / 64; if (arrayIndex >= longCount) { return -1; } long mask = ALL_ONES << index; if ((bits[arrayIndex] & mask) != 0) { return 64 * arrayIndex + Long.numberOfTrailingZeros(bits[arrayIndex] & mask); } while (++arrayIndex < longCount && bits[arrayIndex] == 0) { } if (arrayIndex == longCount) { return -1; } return 64 * arrayIndex + Long.numberOfTrailingZeros(bits[arrayIndex]); }
/** * Sets the bit at index {@code index} to true. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public void set(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= bits.length) { ensureCapacity(arrayIndex + 1); } bits[arrayIndex] |= (1L << index); longCount = Math.max(longCount, arrayIndex + 1); }
/** * Clears the bit at index {@code index}. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public void clear(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= longCount) { return; } bits[arrayIndex] &= ~(1L << index); shrinkSize(); }
/** * Flips the bit at index {@code index}. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public void flip(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= bits.length) { ensureCapacity(arrayIndex + 1); } bits[arrayIndex] ^= (1L << index); longCount = Math.max(longCount, arrayIndex + 1); shrinkSize(); }
/** * Returns the bit at index {@code index}. Indexes greater than the current length return false. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public boolean get(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= longCount) { return false; } return (bits[arrayIndex] & (1L << index)) != 0; }
/** * Returns the bit at index {@code index}. Indexes greater than the current length return false. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public boolean get(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= longCount) { return false; } return (bits[arrayIndex] & (1L << index)) != 0; }
/** * Returns the bit at index {@code index}. Indexes greater than the current length return false. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public boolean get(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= longCount) { return false; } return (bits[arrayIndex] & (1L << index)) != 0; }
public void set(int bitIndex) { checkIndex(bitIndex); set(array, bitIndex); }
public void clear(int bitIndex) { checkIndex(bitIndex); clear(array, bitIndex); }
public boolean get(int bitIndex) { checkIndex(bitIndex); return get(array, bitIndex); }
public void flip(int bitIndex) { checkIndex(bitIndex); flip(array, bitIndex); }
/** * Sets the bit at index {@code index} to true. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public void set(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= bits.length) { ensureCapacity(arrayIndex + 1); } bits[arrayIndex] |= (1L << index); longCount = Math.max(longCount, arrayIndex + 1); }
/** * Sets the bit at index {@code index} to true. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public void set(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= bits.length) { ensureCapacity(arrayIndex + 1); } bits[arrayIndex] |= (1L << index); longCount = Math.max(longCount, arrayIndex + 1); }
/** * Sets the bit at index {@code index} to true. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public void set(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= bits.length) { ensureCapacity(arrayIndex + 1); } bits[arrayIndex] |= (1L << index); longCount = Math.max(longCount, arrayIndex + 1); }
/** * Clears the bit at index {@code index}. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public void clear(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= longCount) { return; } bits[arrayIndex] &= ~(1L << index); shrinkSize(); }
/** * Flips the bit at index {@code index}. * * @throws IndexOutOfBoundsException if {@code index < 0}. */ public void flip(int index) { if (index < 0) { // TODO: until we have an inlining JIT. checkIndex(index); } int arrayIndex = index / 64; if (arrayIndex >= bits.length) { ensureCapacity(arrayIndex + 1); } bits[arrayIndex] ^= (1L << index); longCount = Math.max(longCount, arrayIndex + 1); shrinkSize(); }