Shifting a Java BitSet

后端 未结 8 1595
太阳男子
太阳男子 2020-12-31 00:11

I am using a java.util.BitSet to store a dense vector of bits.

I want to implement an operation that shifts the bits right by 1, analogous to >

8条回答
  •  猫巷女王i
    2020-12-31 00:46

    An alternative which is probably more efficient would be to work with the underlying long[].

    Use bitset.toLongArray() to get the underlying data. Shift those longs accordingly, then create a new BitSet via BitSet.valueOf(long[]) You'll have to be very careful shifting the underlying longs, as you will have to take the low order bit and shift it into the high order bit on the next long in the array.

    This should let you use the bit shift operations native on your processor to move 64 bits at a time, as opposed to iterating through each one separately.

    EDIT: Based on Louis Wasserman's comment. This is only available in Java 1.7 API. Didn't realize that when I wrote it.

提交回复
热议问题