Index: lucene/src/java/org/apache/lucene/util/OpenBitSet.java =================================================================== --- lucene/src/java/org/apache/lucene/util/OpenBitSet.java (revision 1139353) +++ lucene/src/java/org/apache/lucene/util/OpenBitSet.java (working copy) @@ -660,14 +660,16 @@ * -1 is returned if there are no more set bits. */ public int prevSetBit(int index) { - if (index < 0) { - return -1; - } - int i = index>>6; + int i = index >> 6; + final int subIndex; if (i >= wlen) { i = wlen - 1; + if (i < 0) return -1; + subIndex = 0x3f; // last possible bit + } else { + if (i < 0) return -1; + subIndex = index & 0x3f; // index within the word } - final int subIndex = index & 0x3f; // index within the word long word = (bits[i] << (63-subIndex)); // skip all the bits to the left of index if (word != 0) { Index: lucene/src/test/org/apache/lucene/util/TestOpenBitSet.java =================================================================== --- lucene/src/test/org/apache/lucene/util/TestOpenBitSet.java (revision 1139353) +++ lucene/src/test/org/apache/lucene/util/TestOpenBitSet.java (working copy) @@ -42,8 +42,8 @@ } void doPrevSetBit(BitSet a, OpenBitSet b) { - int aa=a.length(); - int bb=aa; + int aa = a.size() + random.nextInt(100); + int bb = aa; do { // aa = a.prevSetBit(aa-1); aa--;