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

booleansetBit(final byte[] buf, final long bitIndex, final boolean value)
Set the value of a bit - this is NOT thread-safe (contention for the byte in the backing buffer can cause lost updates).
final int mask = (1 << withinByteIndexForBit(bitIndex));
final int off = byteIndexForBit(bitIndex);
byte b = buf[off];
final boolean oldValue = (b & mask) != 0;
if (value)
    b |= mask;
else
    b &= ~mask;
...
intsetbit(final int num, final int bitnum, final boolean state)
setbit
return (state) ? (num | (1 << bitnum)) : (num & ~(1 << bitnum));
intsetBit(final int source, final int bit, final boolean value)
Sets the specified bit to 1 if value parameter is true, unsets the bit otherwise.
if (value) {
    return source | (1 << bit);
return source & ~(1 << bit);
longsetBit(final long word, final int idx, final boolean bit)
set Bit
if (bit)
    return setBitTo1(word, idx);
return setBitTo0(word, idx);
intsetBit(int b0, boolean value, int original)
Sets n-th bit for the given integer
if (getBit(b0, original) != value) {
    int mask = (1 << b0);
    return original ^ mask;
return original;
intsetBit(int bit)
set Bit
return (1 << bit);
intsetBit(int bitmask, int bit, boolean enabled)
set Bit
if (enabled) {
    int v = (int) Math.pow(2, bit);
    return bitmask | v;
} else {
    return bitmask & ~bit;
intsetBit(int bits, int index)
Sets bit at index in bits to one.
return bits | (1 << index);
intsetBit(int changeValue, int position, int value)
set Bit
int result = changeValue;
int bt = (1 << position);
if (value == 1) {
    result |= bt;
} else {
    result &= (bt ^ 255);
return result;
...
intsetBit(int data, int bit, int value)
Sets bit in a number
if (value == 1) {
    data = data | (1 << bit);
} else {
    data = data & ~(1 << bit);
return data;