List of utility methods to do Bit Set
byte | setBit(byte bite, int offset, boolean set) Modifies the specified bit within the passed byte to 1 or 0 depending on the boolean passed. if (offset > 7 || offset < 0) { throw new IllegalArgumentException( "Offset must be between 0 and 7!"); if (set) { bite = (byte) (bite | (1 << offset)); } else { bite = (byte) (bite & ~(1 << offset)); ... |
byte | setBit(byte bits, int offset, boolean status) set Bit if (status) { return setBit(bits, offset); } else { return clearBit(bits, offset); |
byte | resetBit(byte b, int bit) reset Bit if (bit < 0 || bit >= 8) return b; return (byte) (b & (~(1 << bit))); |
void | setBit(byte[] arr, int bit) set Bit int index = bit / 8; int bitPosition = bit % 8; arr[index] = (byte) (arr[index] | (1 << bitPosition)); |
void | setBit(byte[] bytes, int bitNr, int bit) set Bit int byteNr = bytes.length - (bitNr / 8) - 1; int bitNrInByte = bitNr % 8; if (bit != 0) { bytes[byteNr] |= 1 << bitNrInByte; } else { bytes[byteNr] &= ~(1 << bitNrInByte); |
void | reverseBits(byte[] bits) reverse Bits for (int i = 0; i < 4; i++) { byte temp = bits[i]; bits[i] = bits[bits.length - i - 1]; bits[bits.length - i - 1] = temp; |
int | bitwiseNot(int number) bitwiseNot int ret = ~number; return ret & 0x0000FFFF; |
byte[] | xor(byte[]... arrays) XORs the bits in an arbitary number of byte arrays. if (arrays.length == 0) return null; sortDecendingSize(arrays); byte[] result = new byte[arrays[0].length]; for (byte[] array : arrays) { for (int i = 0; i < array.length; i++) { result[i] ^= array[i]; return result; |
void | xorArray(byte[] array, byte[] mask) xor Array final int len = Math.min(array.length, mask.length); for (int i = 0; i < len; ++i) { array[i] ^= mask[i]; |