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

voidsetBit(byte[] byteArray, int index)
set Bit
if (index >= byteArray.length * 8 || index < 0) {
    throw new ArrayIndexOutOfBoundsException();
int arrayIndex = index / 8;
index = index % 8;
byte tmpByte = (byte) (0x80 >>> index);
byteArray[arrayIndex] = (byte) (byteArray[arrayIndex] | tmpByte);
voidsetBit(byte[] bytes, int bitNr, int bit)
set Bit
int byteNr = bytes.length - (bitNr / 8) - 1;
int bitNrInByte = bitNr % 8;
if (bit != 0) {
    bytes[byteNr] |= 1 << bitNrInByte;
} else {
    bytes[byteNr] &= ~(1 << bitNrInByte);
voidsetBit(byte[] bytes, int off, boolean v)
set Bit
if (v)
    bytes[off / 8] |= (0x01 << (off % 8));
else
    bytes[off / 8] &= ~(0x01 << (off % 8));
voidsetBit(byte[] data, int index, boolean value)
set Bit
final int MAX = data.length * 8;
if (index >= MAX || index < 0) {
    throw new IndexOutOfBoundsException("Index out of bounds: " + index);
int pos = data.length - index / 8 - 1;
int bitPos = index % 8;
int d = data[pos] & 0xFF;
if (value) {
...
voidsetBit(byte[] data, int pos, boolean val)
set Bit
int posByte = pos / 8;
int posBit = pos % 8;
byte oldByte = data[posByte];
if (val) {
    data[posByte] = (byte) (oldByte | (1 << 7 - posBit));
} else {
    data[posByte] = (byte) (oldByte | (0 << 7 - posBit));
byte[]setBit(byte[] data, int pos, int val)
set Bit
if ((data.length * 8) - 1 < pos)
    throw new Error("outside byte array limit, pos: " + pos);
int posByte = data.length - 1 - (pos) / 8;
int posBit = (pos) % 8;
byte setter = (byte) (1 << (posBit));
byte toBeSet = data[posByte];
byte result;
if (val == 1)
...
voidsetBit(byte[] data, int pos, int val)
set Bit
int posByte = pos / 8;
int posBit = pos % 8;
byte oldByte = data[posByte];
oldByte = (byte) (((0xFF7F >> posBit) & oldByte) & 0x00FF);
byte newByte = (byte) ((val << (8 - (posBit + 1))) | oldByte);
data[posByte] = newByte;
voidsetBit(byte[] data, long pos, byte val)
set Bit
int posByte = (int) (pos / 8);
int posBit = (int) (pos % 8);
byte oldByte = data[posByte];
data[posByte] = setBit(oldByte, posBit, val);
bytesetBit(final byte input, final int bit, final boolean status)
set Bit
if (status)
    return (byte) (input | 1 << bit);
else
    return (byte) (input & ~(1 << bit));
bytesetBit(final byte pData, final int pBitIndex, final boolean pOn)
Method used to set a bit index to 1 or 0.
if (pBitIndex < 0 || pBitIndex > 7) {
    throw new IllegalArgumentException(
            "parameter 'pBitIndex' must be between 0 and 7. pBitIndex=" + pBitIndex);
byte ret = pData;
if (pOn) { 
    ret |= 1 << pBitIndex;
} else { 
...