code: public int nextSetBit(final int i) {
        int x = i / 64;
        if (x >= this.data.limit())
            return -1;
        long w = this.data.get(x);
        w >>>= (i % 64);
        if (w != 0) {
            return i + Long.numberOfTrailingZeros(w);
        }
        ++x;
        for (; x < this.data.limit(); ++x) {
            if (this.data.get(x) != 0) {
                return x
                        * 64
                        + Long.numberOfTrailingZeros(this.data.get(x));
            }
        }
        return -1;
    }
docs: Usage: for(int i=bs.nextSetBit(0); i&gt;=0; i=bs.nextSetBit(i+1)) {
  operate on index i here }
  
  @param i current set bit
  @return next set bit or -1