@Test public void setBitTest() { byte[] data = ByteBuffer.allocate(4).putInt(0).array(); int posBit = 24; int expected = 16777216; int result = -1; byte[] ret = ByteUtil.setBit(data, posBit, 1); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); posBit = 25; expected = 50331648; ret = ByteUtil.setBit(data, posBit, 1); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); posBit = 2; expected = 50331652; ret = ByteUtil.setBit(data, posBit, 1); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); posBit = 24; expected = 33554436; ret = ByteUtil.setBit(data, posBit, 0); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); posBit = 25; expected = 4; ret = ByteUtil.setBit(data, posBit, 0); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); posBit = 2; expected = 0; ret = ByteUtil.setBit(data, posBit, 0); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); }
public static byte[] setBit(byte[] data, int pos, int val) { if ((data.length * 8) - 1 < pos) throw new Error("outside byte array limit, pos: " + pos); int posByte = data.length - 1 - (pos) / 8; int posBit = (pos) % 8; byte setter = (byte) (1 << (posBit)); byte toBeSet = data[posByte]; byte result; if (val == 1) result = (byte) (toBeSet | setter); else result = (byte) (toBeSet & ~setter); data[posByte] = result; return data; }
ByteUtil { public static byte[] setBit(byte[] data, int pos, int val) { if ((data.length * 8) - 1 < pos) throw new Error("outside byte array limit, pos: " + pos); int posByte = data.length - 1 - (pos) / 8; int posBit = (pos) % 8; byte setter = (byte) (1 << (posBit)); byte toBeSet = data[posByte]; byte result; if (val == 1) result = (byte) (toBeSet | setter); else result = (byte) (toBeSet & ~setter); data[posByte] = result; return data; } }
ByteUtil { public static byte[] setBit(byte[] data, int pos, int val) { if ((data.length * 8) - 1 < pos) throw new Error("outside byte array limit, pos: " + pos); int posByte = data.length - 1 - (pos) / 8; int posBit = (pos) % 8; byte setter = (byte) (1 << (posBit)); byte toBeSet = data[posByte]; byte result; if (val == 1) result = (byte) (toBeSet | setter); else result = (byte) (toBeSet & ~setter); data[posByte] = result; return data; }  }
ByteUtil { public static byte[] setBit(byte[] data, int pos, int val) { if ((data.length * 8) - 1 < pos) throw new Error("outside byte array limit, pos: " + pos); int posByte = data.length - 1 - (pos) / 8; int posBit = (pos) % 8; byte setter = (byte) (1 << (posBit)); byte toBeSet = data[posByte]; byte result; if (val == 1) result = (byte) (toBeSet | setter); else result = (byte) (toBeSet & ~setter); data[posByte] = result; return data; }  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 byte[] setBit(byte[] data, int pos, int val) { if ((data.length * 8) - 1 < pos) throw new Error("outside byte array limit, pos: " + pos); int posByte = data.length - 1 - (pos) / 8; int posBit = (pos) % 8; byte setter = (byte) (1 << (posBit)); byte toBeSet = data[posByte]; byte result; if (val == 1) result = (byte) (toBeSet | setter); else result = (byte) (toBeSet & ~setter); data[posByte] = result; return data; }  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; }