@Test public void testNumberOfLeadingZeros() { int n0 = ByteUtil.numberOfLeadingZeros(new byte[0]); assertEquals(0, n0); int n1 = ByteUtil.numberOfLeadingZeros(Hex.decode("05")); assertEquals(5, n1); int n2 = ByteUtil.numberOfLeadingZeros(Hex.decode("01")); assertEquals(7, n2); int n3 = ByteUtil.numberOfLeadingZeros(Hex.decode("00")); assertEquals(8, n3); int n4 = ByteUtil.numberOfLeadingZeros(Hex.decode("ff")); assertEquals(0, n4); byte[] v1 = Hex.decode("1040"); int n5 = ByteUtil.numberOfLeadingZeros(v1); assertEquals(3, n5); byte[] v2 = new byte[4]; System.arraycopy(v1, 0, v2, 2, v1.length); int n6 = ByteUtil.numberOfLeadingZeros(v2); assertEquals(19, n6); byte[] v3 = new byte[8]; int n7 = ByteUtil.numberOfLeadingZeros(v3); assertEquals(64, n7); }
public static int numberOfLeadingZeros(byte[] bytes) { int i = firstNonZeroByte(bytes); if (i == -1) { return bytes.length * 8; } else { int byteLeadingZeros = Integer.numberOfLeadingZeros((int)bytes[i] & 0xff) - 24; return i * 8 + byteLeadingZeros; } }
ByteUtil { public static int numberOfLeadingZeros(byte[] bytes) { int i = firstNonZeroByte(bytes); if (i == -1) { return bytes.length * 8; } else { int byteLeadingZeros = Integer.numberOfLeadingZeros((int)bytes[i] & 0xff) - 24; return i * 8 + byteLeadingZeros; } } }
ByteUtil { public static int numberOfLeadingZeros(byte[] bytes) { int i = firstNonZeroByte(bytes); if (i == -1) { return bytes.length * 8; } else { int byteLeadingZeros = Integer.numberOfLeadingZeros((int)bytes[i] & 0xff) - 24; return i * 8 + byteLeadingZeros; } }  }
ByteUtil { public static int numberOfLeadingZeros(byte[] bytes) { int i = firstNonZeroByte(bytes); if (i == -1) { return bytes.length * 8; } else { int byteLeadingZeros = Integer.numberOfLeadingZeros((int)bytes[i] & 0xff) - 24; return i * 8 + byteLeadingZeros; } }  static byte[] appendByte(byte[] bytes, byte b); static byte[] bigIntegerToBytes(BigInteger b, int numBytes); static byte[] bigIntegerToBytesSigned(BigInteger b, int numBytes); static byte[] bigIntegerToBytes(BigInteger value); static BigInteger bytesToBigInteger(byte[] bb); static int matchingNibbleLength(byte[] a, byte[] b); static byte[] longToBytes(long val); static byte[] longToBytesNoLeadZeroes(long val); static byte[] intToBytes(int val); static byte[] intToBytesNoLeadZeroes(int val); static String toHexString(byte[] data); static byte[] calcPacketLength(byte[] msg); static int byteArrayToInt(byte[] b); static long byteArrayToLong(byte[] b); static String nibblesToPrettyString(byte[] nibbles); static String oneByteToHexString(byte value); static int numBytes(String val); static byte[] encodeValFor32Bits(Object arg); static byte[] encodeDataList(Object... args); static int firstNonZeroByte(byte[] data); static byte[] stripLeadingZeroes(byte[] data); static boolean increment(byte[] bytes); static byte[] copyToArray(BigInteger value); static ByteArrayWrapper wrap(byte[] data); static byte[] setBit(byte[] data, int pos, int val); static int getBit(byte[] data, int pos); static byte[] and(byte[] b1, byte[] b2); static byte[] or(byte[] b1, byte[] b2); static byte[] xor(byte[] b1, byte[] b2); static byte[] xorAlignRight(byte[] b1, byte[] b2); static byte[] merge(byte[]... arrays); static boolean isNullOrZeroArray(byte[] array); static boolean isSingleZero(byte[] array); static Set<byte[]> difference(Set<byte[]> setA, Set<byte[]> setB); static int length(byte[]... bytes); static byte[] intsToBytes(int[] arr, boolean bigEndian); static int[] bytesToInts(byte[] arr, boolean bigEndian); static void bytesToInts(byte[] b, int[] arr, boolean bigEndian); static void intsToBytes(int[] arr, byte[] b, boolean bigEndian); static short bigEndianToShort(byte[] bs); static short bigEndianToShort(byte[] bs, int off); static byte[] shortToBytes(short n); static byte[] hexStringToBytes(String data); static byte[] hostToBytes(String ip); static String bytesToIp(byte[] bytesIp); static int numberOfLeadingZeros(byte[] bytes); static byte[] parseBytes(byte[] input, int offset, int len); static byte[] parseWord(byte[] input, int idx); static byte[] parseWord(byte[] input, int offset, int idx); }
ByteUtil { public static int numberOfLeadingZeros(byte[] bytes) { int i = firstNonZeroByte(bytes); if (i == -1) { return bytes.length * 8; } else { int byteLeadingZeros = Integer.numberOfLeadingZeros((int)bytes[i] & 0xff) - 24; return i * 8 + byteLeadingZeros; } }  static byte[] appendByte(byte[] bytes, byte b); static byte[] bigIntegerToBytes(BigInteger b, int numBytes); static byte[] bigIntegerToBytesSigned(BigInteger b, int numBytes); static byte[] bigIntegerToBytes(BigInteger value); static BigInteger bytesToBigInteger(byte[] bb); static int matchingNibbleLength(byte[] a, byte[] b); static byte[] longToBytes(long val); static byte[] longToBytesNoLeadZeroes(long val); static byte[] intToBytes(int val); static byte[] intToBytesNoLeadZeroes(int val); static String toHexString(byte[] data); static byte[] calcPacketLength(byte[] msg); static int byteArrayToInt(byte[] b); static long byteArrayToLong(byte[] b); static String nibblesToPrettyString(byte[] nibbles); static String oneByteToHexString(byte value); static int numBytes(String val); static byte[] encodeValFor32Bits(Object arg); static byte[] encodeDataList(Object... args); static int firstNonZeroByte(byte[] data); static byte[] stripLeadingZeroes(byte[] data); static boolean increment(byte[] bytes); static byte[] copyToArray(BigInteger value); static ByteArrayWrapper wrap(byte[] data); static byte[] setBit(byte[] data, int pos, int val); static int getBit(byte[] data, int pos); static byte[] and(byte[] b1, byte[] b2); static byte[] or(byte[] b1, byte[] b2); static byte[] xor(byte[] b1, byte[] b2); static byte[] xorAlignRight(byte[] b1, byte[] b2); static byte[] merge(byte[]... arrays); static boolean isNullOrZeroArray(byte[] array); static boolean isSingleZero(byte[] array); static Set<byte[]> difference(Set<byte[]> setA, Set<byte[]> setB); static int length(byte[]... bytes); static byte[] intsToBytes(int[] arr, boolean bigEndian); static int[] bytesToInts(byte[] arr, boolean bigEndian); static void bytesToInts(byte[] b, int[] arr, boolean bigEndian); static void intsToBytes(int[] arr, byte[] b, boolean bigEndian); static short bigEndianToShort(byte[] bs); static short bigEndianToShort(byte[] bs, int off); static byte[] shortToBytes(short n); static byte[] hexStringToBytes(String data); static byte[] hostToBytes(String ip); static String bytesToIp(byte[] bytesIp); static int numberOfLeadingZeros(byte[] bytes); static byte[] parseBytes(byte[] input, int offset, int len); static byte[] parseWord(byte[] input, int idx); static byte[] parseWord(byte[] input, int offset, int idx); static final byte[] EMPTY_BYTE_ARRAY; static final byte[] ZERO_BYTE_ARRAY; }