Bit Manipulation¶
Examples¶
Java¶
Get¶
boolean getBit(int num, int i) {
return ((num & (1 << i)) != 0);
}
Set¶
int setBit(int num, int i) {
return num | (1 << i);
}
Clear¶
int clearBit(int num, int i) {
int mask = ~(1 << i);
return num & mask;
}
Update¶
int updateBit(int num, int i, int v) {
int mask = ~(1 << i);
return (num & mask) | (v << i);
}