@Test public void testBitsBytesConvert() { Random r = new Random(); boolean[] bits = new boolean[32]; for (int i = 0; i < bits.length; i++) { bits[i] = r.nextBoolean(); } byte[] bytes = new byte[4]; for (int i = 0;i < bits.length; i++) { if (bits[i]) { bytes[i/8] |= (1 << 7) >> (i%8); } } for (int i = 0; i < bits.length; i++) { if (Convert.getNthBit(bytes, i) != bits[i]) { fail("Conversion of bytes to bits failed at bit "+i); } } }
public static boolean getNthBit(byte[] arr, int offset){ int arrayOffset = offset / 8; int bitOfByte = offset % 8; int maskedBit = arr[arrayOffset] & (1 << (7 - bitOfByte)); return (maskedBit != 0); }
Convert { public static boolean getNthBit(byte[] arr, int offset){ int arrayOffset = offset / 8; int bitOfByte = offset % 8; int maskedBit = arr[arrayOffset] & (1 << (7 - bitOfByte)); return (maskedBit != 0); } }
Convert { public static boolean getNthBit(byte[] arr, int offset){ int arrayOffset = offset / 8; int bitOfByte = offset % 8; int maskedBit = arr[arrayOffset] & (1 << (7 - bitOfByte)); return (maskedBit != 0); }  }
Convert { public static boolean getNthBit(byte[] arr, int offset){ int arrayOffset = offset / 8; int bitOfByte = offset % 8; int maskedBit = arr[arrayOffset] & (1 << (7 - bitOfByte)); return (maskedBit != 0); }  static String bytesToHex(byte[] bytes); static byte[] strToBytes(String str); static byte[] longToBytes(long val); static boolean getNthBit(byte[] arr, int offset); static byte[] getPrefixBytes(byte[] arr); }
Convert { public static boolean getNthBit(byte[] arr, int offset){ int arrayOffset = offset / 8; int bitOfByte = offset % 8; int maskedBit = arr[arrayOffset] & (1 << (7 - bitOfByte)); return (maskedBit != 0); }  static String bytesToHex(byte[] bytes); static byte[] strToBytes(String str); static byte[] longToBytes(long val); static boolean getNthBit(byte[] arr, int offset); static byte[] getPrefixBytes(byte[] arr);  }