public int nextSetBit(int fromIndex) {
        if (fromIndex < 0)
            throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);

        int segmentPosition = fromIndex >>> log2SegmentSize; /// which segment -- div by num bits per segment

        ThreadSafeBitSetSegments segments = this.segments.get();

        if(segmentPosition >= segments.numSegments())
            return -1;

        int longPosition = (fromIndex >>> 6) & segmentMask; /// which long in the segment -- remainder of div by num bits per segment
        int bitPosition = fromIndex & 0x3F; /// which bit in the long -- remainder of div by num bits in long (64)
        AtomicLongArray segment = segments.getSegment(segmentPosition);

        long word = segment.get(longPosition) & (0xffffffffffffffffL << bitPosition);

        while (true) {
            if (word != 0)
                return (segmentPosition << (log2SegmentSize)) + (longPosition << 6) + Long.numberOfTrailingZeros(word);
            if (++longPosition > segmentMask) {
                segmentPosition++;
                if(segmentPosition >= segments.numSegments())
                    return -1;
                segment = segments.getSegment(segmentPosition);
                longPosition = 0;
            }

            word = segment.get(longPosition);
        }
    }
@Test
    public void testNextSetBit() {
        ThreadSafeBitSet set1 = new ThreadSafeBitSet();

        set1.set(100);
        set1.set(101);
        set1.set(103);
        set1.set(100000);
        set1.set(1000000);

        Assert.assertEquals(100, set1.nextSetBit(0));
        Assert.assertEquals(101, set1.nextSetBit(101));
        Assert.assertEquals(103, set1.nextSetBit(102));
        Assert.assertEquals(100000, set1.nextSetBit(104));
        Assert.assertEquals(1000000, set1.nextSetBit(100001));
        Assert.assertEquals(-1, set1.nextSetBit(1000001));
        Assert.assertEquals(-1, set1.nextSetBit(1015809));

        set1.clearAll();
        set1.set(555555);
        Assert.assertEquals(555555, set1.nextSetBit(0));
        Assert.assertEquals(-1, set1.nextSetBit(555556));
    }