title: Java BitSet with trailing zeros
body: <p>How do I work with BitSet for bit representations ending with 0s?</p>
  <p>For example, to represent &quot;10100&quot; in BitSet, I'm doing the following.</p>
  <pre><code>BitSet bits = new BitSet(5);
  bits.set(0);
  bits.set(2);
  </code></pre>
  <p>Based on the Java Doc,</p>
  <blockquote>
  <p>length() - Returns the &quot;logical size&quot; of this BitSet: the index of the highest set bit in the BitSet plus one.</p>
  <p>size() - Returns the number of bits of space actually in use by this BitSet to represent bit values.</p>
  </blockquote>
  <p>So, for the given example length() returns &quot;3&quot; and size() returns &quot;64&quot; as BitSet internally works with long.</p>
  <p>With the given BitSet, how can I determine the actual bits (10100 in this case) in it?</p>
  <p>P.S: I'm working on compression techniques and I don't want to use boolean[] to represent it, as each entry in the array can take 1 byte.</p>
  <p>Thank you!</p>