ByteUtils { public static int readBit(byte value, int position) { if (position > 7 || position < 0) { throw new ArrayIndexOutOfBoundsException("position must be 0 - 7."); } byte[] data = new byte[2]; data[1] = value; int dataValue = new BigInteger(data).intValue(); return dataValue >>> position; }  static byte[][] split(byte[] input, int firstLength); static byte[] reverse(byte[] input); static byte[] join(byte[] value1, byte[] value2); static byte[] join(byte[]... inputs); static byte[] paddingZeroOnHead(byte[] input, int byteLength); static byte[] paddingZeroOnTail(byte[] input, int byteLength); static int readBit(byte value, int position);  }
@Test public void success_ReadBit() { byte value = 0b01010101; int readBit1 = ByteUtils.readBit(value, 7); int readBit2 = ByteUtils.readBit(value, 6); assertThat(readBit1, is(0)); assertThat(readBit2, is(1)); }
@Test public void success_ReadBit_but_do_not_modify_original() { byte value = (byte) 0b11010101; int readBit = ByteUtils.readBit(value, 7); assertThat(readBit, is(1)); assertThat(value, is((byte) 0b11010101)); }
@Test(expected = ArrayIndexOutOfBoundsException.class) public void failure_ReadBit_1() { byte value = 0b01010101; ByteUtils.readBit(value, -1); }
@Test(expected = ArrayIndexOutOfBoundsException.class) public void failure_ReadBit_2() { byte value = 0b01010101; ByteUtils.readBit(value, 8); }