AND bit by bit
Shift on the left
n * 2 <=> left shift by 1
n * 4 <=> left shift by 2
Shift on the right
Logical shift (shift the sign bit as well)
XOR bit by bit
Vector (linear sequence of numeric values stored contiguously in memory) in which each element is a bit (so either 0 or 1)
boolean checkExactlyOneBitSet(int num) {
return num != 0 && (num & (num - 1)) == 0;
}
int clearBitsFromITo0(int num, int i) {
int mask = (-1 << (i + 1));
return num & mask;
}
int clearBitsFromMsbToI(int num, int i) {
int mask = (1 << i) - 1;
return num & mask;
}
int clearBit(final int num, final int i) {
final int mask = ~(1 << i);
return num & mask;
}
int flipBit(final int num, final int i) {
return num ^ (1 << i);
}
boolean getBit(final int num, final int i) {
return ((num & (1 << i)) != 0);
}
b ^ 1
Use the most significative bit to represent the sign. Yet, it is not enough (problem with this technique: 5 + (-5) != 0)
Two's complement technique: take the one complement and add one
-3: 1101
-2: 1110
-1: 1111
0: 0000
1: 0001
2: 0010
3: 0011
The most significant bit still represents the sign
Max integer value: 1...1 (31 bits)
-1: 1...1 (32 bits)
int setBit(final int num, final int i) {
return num | (1 << i);
}
- Clear this bit
- Apply OR on the result with a 0 or 1 left shifted to its index
int updateBit(int num, int i, boolean bit) {
int value = bit ? 1 : 0;
int mask = ~(1 << i);
return (num & mask) | (value << i);
}
0
x
x
x
~x
0
x
1s
x
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
n XOR 0 => keep
n XOR 1 => flip
OR bit by bit
Complement bit by bit