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

intsetBit(int value, int bit)
Set the bit within the int.
assert bit >= 0 && bit < 16;
return value | bitMask(bit);
intsetBit(int value, int bitIndex)
set Bit
assert (bitIndex >= 0);
assert (bitIndex <= 31);
return value | pow2(bitIndex);
intsetBit(int value, int bitmask, boolean set)
set Bit
if (set) {
    return bitmask | value;
} else {
    System.out.println("Reset bit " + value + ": from " + bitmask + " to " + (bitmask & ~value));
    return bitmask & ~value;
intsetBit(int value, int flags)
Sets the bits for value using 'bitwise or' operation and return new modified value.
return (flags | value);
intsetBit(int value, int index)
Sets a bit of a value at a given position.
return value | (1 << index);
voidsetBit(int[] x, int i, int v)
Used to set or clear a bit in an int array
if ((v & 0x01) == 1)
    x[i / 32] |= 1 << (i % 32); 
else
    x[i / 32] &= ~(1 << (i % 32)); 
voidsetBit(long[] data, int position, int bitWidth)
Set the bit with the position received as an argument to the multi-dimension point received as an argument.
int dimIndex = position % data.length;
int bitIndex = bitWidth - 1 - position / data.length;
data[dimIndex] = data[dimIndex] | (1 << bitIndex);
shortsetBit(short shortIn, int bitPos)
set Bit
short mask = (short) (1 << bitPos);
return (short) (shortIn | mask);
bytesetBitAt(byte flags, int pos, boolean value)
Modifies the value of the bit specified by its position in the given byte.
Note that the return value has to be used as the result of the operation, since the method operates on a copy of the input byte.
if (value) {
    switch (pos) {
    case 0:
        flags |= (byte) 0x01;
        break;
    case 1:
        flags |= (byte) 0x02;
        break;
...
bytesetBitAt(int bitIndex, boolean value, byte b)
bitIndex from right to left (0 = least significant = rightmost bit)
if (value)
    return (byte) ((int) b | (1 << (bitIndex & 31)));
else
    return (byte) ((int) b & ~(1 << (bitIndex & 31)));