Java Utililty Methods Bit Set

List of utility methods to do Bit Set

Description

The list of methods to do Bit Set are organized into topic(s).

Method

bytesetBit(byte input, int bit)
set Bit
return (byte) (input | BYTE[bit]);
bytesetBit(byte original, int bitToSet)
Sets a bit to one in a byte then returns new byte
return (byte) (original | (1 << bitToSet));
bytesetBit(byte target, byte pos)
set Bit
return target |= (1 << pos);
bytesetBit(byte v, int position, boolean value)
Returns v, with the bit at position set to 1 or 0 depending on value.
return (byte) setBit((int) v, position, value);
bytesetBit(byte value, int bit, boolean on)
sets the specified bit to a value
if (bit < 0 || bit >= 8)
    throw new IllegalArgumentException("invalid bit position");
byte pos = (byte) (1 << bit);
if (on)
    return (byte) (value | pos);
else
    return (byte) (value & ~pos);
bytesetBit(byte value, int bit, boolean state)
Sets a single bit in a byte value
return (byte) setBit((int) value, bit, state);
bytesetBit(byte value, int bit, boolean state)
Sets the given bit in the given value.
return state ? (byte) (value | bit) : (byte) (value & ~bit);
voidsetBit(byte[] arr, int bit)
set Bit
int index = bit / 8; 
int bitPosition = bit % 8; 
arr[index] = (byte) (arr[index] | (1 << bitPosition));
byte[]setBit(byte[] b, int index)
Sets the bit at the specified index of b to 1.
int maxIndex = b.length * 8 - 1;
if ((index < 0) || (index > maxIndex)) {
    throw new IndexOutOfBoundsException(
            "Invalid bit index: " + index + " for " + b.length + "-byte array.");
int byteOffset = b.length - (index / 8) - 1;
int bitOffset = index % 8;
byte mask = (byte) (0xFF & (1 << bitOffset));
...
voidsetBit(byte[] ba, int bit_offset, boolean on)
set Bit
int byte_idx = bit_offset / 8;
int bit_remainder = bit_offset % 8;
byte bit_mask = (byte) ((0x1 << (7 - bit_remainder)) & 0xFF);
if (on) {
    ba[byte_idx] |= bit_mask;
} else {
    ba[byte_idx] &= ~bit_mask;