Shifting a Java BitSet

后端 未结 8 1571
太阳男子
太阳男子 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条回答
  •  甜味超标
    2020-12-31 00:59

    With java SE8, it can be achieved more concise way:

    BitSet b = new BitSet();
    b.set(1, 3);
    BitSet shifted = BitSet.valueOf(Arrays.stream(
           b.toLongArray()).map(v -> v << 1).toArray());
    

    I was trying to figure out how to use LongBuffer to do so but not quite got it to work. Hopefully, someone who is familiar with low level programming can point out a solution.

    Thanks in advance!!!

提交回复
热议问题