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

bytesetBitAt(int offset, boolean bitValue, byte aByte)
Set a bit within a byte.
return (byte) ((bitValue) ? (aByte | (1 << offset)) : (aByte & ~(1 << offset)));
bytesetBitAt(int offset, boolean bitValue, byte aByte)
set Bit At
return (byte) ((bitValue) ? (aByte | (1 << offset)) : (aByte & ~(1 << offset)));
intsetBitBiInt(int b0, int b1, int value, int original)
Sets an int into another int.
original = setBit(b0, getBit(0, value), original);
original = setBit(b1, getBit(1, value), original);
return original;
bytesetBitByPos(byte byt, boolean bool, int pos)
set Bit By Pos
if (bool) {
    return (byte) ((int) byt | mask[pos]);
} else {
    return (byte) ((int) byt & invMask[pos]);
intsetBitInInt(int bits, int bitIndex, boolean flag)
Convenience method to set a boolean as a bit in the specified int, for memory utilisation purposes.
if (bitIndex < 0 || bitIndex > 31) {
    throw new IllegalArgumentException();
int mask = 1 << bitIndex;
return (bits & ~mask) | (flag ? mask : 0);
longsetBitInLong(long l, long n, int v)
Set bit n of a 64-bit sequence i to v (0 or 1)
if (v != 0) {
    return l | (1L << n); 
} else {
    return l & ~(1L << n); 
voidsetBitLE(byte[] data, int index)
set Bit LE
data[index >>> 3] |= bitMask[7 & index];
voidsetBitmapRange(long[] bitmap, int start, int end)
set bits at start, start+1,..., end-1
if (start == end) {
    return;
int firstword = start / 64;
int endword = (end - 1) / 64;
if (firstword == endword) {
    bitmap[firstword] |= (~0L << start) & (~0L >>> -end);
    return;
...
intsetBitmapRangeAndCardinalityChange(long[] bitmap, int start, int end)
set bits at start, start+1,..., end-1 and report the cardinality change
int cardbefore = cardinalityInBitmapWordRange(bitmap, start, end);
setBitmapRange(bitmap, start, end);
int cardafter = cardinalityInBitmapWordRange(bitmap, start, end);
return cardafter - cardbefore;
intsetBitRange(final int val, final int start, final int len, final int newVal)
Sets a set of bits in a int.
int mask = ((1 << len) - 1) << start;
return (val & ~mask) | ((newVal << start) & mask);