class BitManipulation {
static and(a, b) {
return a & b; // bitwise AND
}
static or(a, b) {
return a | b; // bitwise OR
}
static xor(a, b) {
return a ^ b; // bitwise XOR
}
static not(a) {
return ~a; // bitwise NOT
}
static leftShift(a, n) {
return a << n; // left shift by n bits
}
static rightShift(a, n) {
return a >> n; // right shift by n bits
}
}
// Driver code using input from dry run
const a = 6; // 00000110
const b = 3; // 00000011
console.log('AND:', BitManipulation.and(a, b)); // 2
console.log('OR:', BitManipulation.or(a, b)); // 7
console.log('XOR:', BitManipulation.xor(a, b)); // 5
console.log('NOT a:', BitManipulation.not(a)); // -7
console.log('Left shift a << 1:', BitManipulation.leftShift(a, 1)); // 12
console.log('Right shift a >> 1:', BitManipulation.rightShift(a, 1)); // 3return a & b; // bitwise AND
combine bits keeping only those on in both numbers
return a | b; // bitwise OR
combine bits turning on if either number has bit on
return a ^ b; // bitwise XOR
combine bits turning on only if bits differ
return ~a; // bitwise NOT
flip every bit of the number
return a << n; // left shift by n bits
move bits left by n places, filling with zeros
return a >> n; // right shift by n bits
move bits right by n places, filling with sign bit
AND: 2
OR: 7
XOR: 5
NOT a: -7
Left shift a << 1: 12
Right shift a >> 1: 3