BitSetMath { public static void nonNegativeLongToLittleEndianBitSet(long value, BitSet bs) { Preconditions.checkArgument(value >= 0, "Negative numbers are not allowed."); bs.clear(); int bitLength = 64 - Long.numberOfLeadingZeros(value); assert bitLength < 64 : "How could negative number " + value + " get here?"; if (bitLength != 0) { int lowestSetBit = Long.numberOfTrailingZeros(value); assert lowestSetBit != 64; for (int i = lowestSetBit; i < bitLength; ++i) { if ((value & (1L << i)) != 0) { bs.set(i); } } } } private  BitSetMath(); static int increment(BitSet i); static void rotate(BitSet bs, int n, int count); static void extractBitRange(BitSet chi, int from, int to, BitSet r); static void grayCode(BitSet i); static void grayCodeInverse(BitSet i); static void copy(BitSet from, BitSet to); static long littleEndianBitSetToNonNegativeLong(BitSet bs); static void nonNegativeLongToLittleEndianBitSet(long value, BitSet bs);  }
@Test public void nonNegativeLongToLittleEndianBitSet() { for (int i = 0; i < 1024; ++i) { BitSet expected = TestUtils.unsignedIntToLittleEndianBitSet(i); BitSet actual = new BitSet(); BitSetMath.nonNegativeLongToLittleEndianBitSet(i, actual); Assert.assertEquals(expected, actual); } }