const a = 6; // binary 110 const b = 3; // binary 011 const result = (a & b) * 2; console.log(result);
The bitwise AND of 6 (110) and 3 (011) is 2 (010). Multiplying 2 by 2 gives 4. So the code prints 4, making option B correct.
const x = 5; const y = x << 1; console.log(y);
Left shifting 5 (binary 101) by 1 bit gives 10 (binary 1010). So the output is 10.
let num = 12; // binary 1100 num = num ^ 5; // binary 0101 console.log(num);
12 in binary is 1100, 5 is 0101. XOR toggles bits where 5 has 1s:
- 1100 XOR 0101 = 1001 (binary) = 9 decimal
So output is 9.
Bit shifts multiply or divide by powers of two using simple binary shifts, which are faster than arithmetic multiplication or division.
Other options are incorrect because bitwise AND is not addition, bitwise operations don't calculate square roots, and bitwise OR does not concatenate strings.
The least significant bit (rightmost bit) is 0 for even numbers and 1 for odd numbers.
Using (n & 1) extracts this bit. If it is 0, the number is even.
Other options misuse bitwise operators and do not correctly check evenness.
