Сдвиг Java BitSet

Я использую java.util.BitSet для хранения плотного вектора битов.

Я хочу реализовать операцию, которая сдвигает биты вправо на 1, аналогично >>> в int.

Есть ли библиотечная функция, которая сдвигает BitSet s?

Если нет, есть ли способ лучше, чем приведенный ниже?

public static void logicalRightShift(BitSet bs) {
  for (int i = 0; (i = bs.nextSetBit(i)) >= 0;) {
    // i is the first bit in a run of set bits.

    // Set any bit to the left of the run.
    if (i != 0) { bs.set(i - 1); }

    // Now i is the index of the bit after the end of the run.
    i = bs.nextClearBit(i);  // nextClearBit never returns -1.
    // Clear the last bit of the run.
    bs.clear(i - 1);

    // 0000111100000...
    //     a   b
    // i starts off the loop at a, and ends the loop at b.
    // The mutations change the run to
    // 0001111000000...
  }
}
20
задан Mike Samuel 25 January 2012 в 22:36
поделиться