On Fri, Jun 24, 2011 at 12:14 PM, Yonik Seeley <[email protected]> wrote: > All that needs to be done is to move the negative index check to the > bottom (the first index<0 is not needed since we do a signed shift). > > public int prevSetBit(int index) { > int i = index>>6; > if (i >= wlen) { > i = wlen - 1; > } > if (i < 0) return -1; > final int subIndex = index & 0x3f; // index within the word > long word = (bits[i] << (63-subIndex)); // skip all the bits to > the left of index
And a further minor optimization, if we assume that negative indexes are not legal, is to move the (i<0) check inside the if (i>=wlen) block (and just let a negative index passed by the user to cause a natural AIOOB). -Yonik http://www.lucidimagination.com --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
