ByteAndBitConverter { public static BitSet intToBitSet(int i) { BitSet bs = new BitSet(Integer.SIZE); for (int k = 0; k < Integer.SIZE; k++) { if ((i & (1 << k)) != 0) { bs.set(k); } } return bs; } private  ByteAndBitConverter(); static byte[] toByteArray(long value); static byte[] toByteArray(int value); static BitSet intToBitSet(int i); static Boolean[] toBoolean(String hex); static String toHex(boolean[] bits); static String toHex(List<Boolean> bits);  }
@Test public void testIntToBitSet() { BitSet x = ByteAndBitConverter.intToBitSet(5); BitSet expectedX = new BitSet(Integer.SIZE); expectedX.set(0); expectedX.set(2); Assert.assertEquals(expectedX, x); BitSet y = ByteAndBitConverter.intToBitSet(0); BitSet expectedY = new BitSet(Integer.SIZE); Assert.assertEquals(expectedY, y); BitSet z = ByteAndBitConverter.intToBitSet(-9); BitSet expectedZ = new BitSet(Integer.SIZE); expectedZ.set(3); expectedZ.flip(0, Integer.SIZE); Assert.assertEquals(expectedZ, z); }