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

bytesetBits(byte in, byte data, int position, int fillBits)
set Bits
if (position - fillBits == 0) {
    fillBits++;
in |= data << 9 - position - fillBits;
return in;
bytesetBits(final byte value, final int bitMask, final boolean val)
set Bits
byte mask = (byte) (0xFF ^ bitMask);
byte tmp = (byte) (value & mask);
if (val) {
    tmp |= bitMask;
return tmp;
intsetBits(int lowBit, int numBits)
Set numBits bits in an integer starting at lowBit and working higher.
int result = 1;
for (int i = 0; i < numBits - 1; ++i) {
    result = (result << 1) | 1;
for (int i = 0; i < lowBit; ++i) {
    result <<= 1;
return result;
...
intsetBits(int value, int bits)
Set selected bit(s) in giving value
return value | bits;
longsetBits(long value, long bits)
Sets the bits of a value.
return value | bits;
voidsetBitsFromLong(byte[] dst, long dstoff, long l, int off, int len)
set Bits From Long
int shift = off;
while (len > 0) {
    int bitoff = (int) dstoff & 7;
    int dstByteOff = (int) (dstoff >> 3);
    if (bitoff == 0 && len >= 8) {
        do {
            dst[dstByteOff] = (byte) ((l >>> shift) & 0xff);
            shift += 8;
...
longsetBitsInLong(long l, int n, int k, long v)
Return a 'long' value with k bits starting at bit n assigned according to v I.e.
long temp = ~(~0L << k);
long temp2 = ~(temp << n);
long temp3 = l & temp2;
return temp3 | (v << n);
longsetBitTo0(final long word, final int idx)
set Bit To
return (~(1l << idx) & word);
intsetBitValue(byte aByte, int bitIndex, int bitValue)
Sets the bit value of the provided byte at the provided bitIndex (0 <= bitIndex <=7) to the provided bitValue(0 or 1).
if (bitIndex >= BITS_PER_BYTE) {
    throw new ArrayIndexOutOfBoundsException("The provided bitIndex[" + bitIndex
            + "] is larger than the size of a byte[" + BITS_PER_BYTE + "].");
if (bitIndex < 0) {
    throw new ArrayIndexOutOfBoundsException(
            "The provided bitIndex[" + bitIndex + "] must be greater than or equal to zero.");
byte newByte = (byte) 0;
if (bitValue == 0) {
    newByte = (byte) (aByte & ~(1 << bitIndex));
} else if (bitValue == 1) {
    newByte = (byte) (aByte | (1 << bitIndex));
} else {
    throw new IllegalArgumentException("The provided bitValue[" + bitValue + "] must be either 0 or 1.");
return newByte;
intsetBitValueAt(int destination, int value, int index)
set Bit Value At
return destination | value << index;