title: Shifting a Java BitSet
body: <p>I am using a <code>java.util.BitSet</code> to store a dense vector of bits.</p>
  
  <p>I want to implement an operation that shifts the bits right by 1, analogous to <code>&gt;&gt;&gt;</code> on ints.</p>
  
  <p>Is there a library function that shifts <code>BitSet</code>s?</p>
  
  <p>If not, is there a better way than the below?</p>
  
  <pre><code>public static void logicalRightShift(BitSet bs) {
    for (int i = 0; (i = bs.nextSetBit(i)) &gt;= 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...
    }
  }
  </code></pre>