Challenge - 5 Problems
Bitwise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bitwise AND and OR Operations
What is the output of the following JavaScript code?
const a = 5; // binary 0101 const b = 3; // binary 0011 const result = (a & b) | (a | b); console.log(result);
DSA Javascript
const a = 5; // binary 0101 const b = 3; // binary 0011 const result = (a & b) | (a | b); console.log(result);
Attempts:
2 left
💡 Hint
Remember how AND (&) and OR (|) work bit by bit.
✗ Incorrect
AND of 5 (0101) and 3 (0011) is 1 (0001). OR of 5 and 3 is 7 (0111). Then OR of 1 and 7 is 7.
❓ Predict Output
intermediateEffect of XOR on Bits
What will be printed by this JavaScript code?
const x = 12; // binary 1100 const y = 10; // binary 1010 const z = x ^ y; console.log(z);
DSA Javascript
const x = 12; // binary 1100 const y = 10; // binary 1010 const z = x ^ y; console.log(z);
Attempts:
2 left
💡 Hint
XOR (^) sets bits where bits differ.
✗ Incorrect
12 (1100) XOR 10 (1010) is 0110 in binary, which is 6 in decimal.
❓ Predict Output
advancedUnderstanding NOT and Left Shift
What is the output of this JavaScript code?
const num = 4; // binary 00000100 const result = (~num) << 1; console.log(result);
DSA Javascript
const num = 4; // binary 00000100 const result = (~num) << 1; console.log(result);
Attempts:
2 left
💡 Hint
Remember NOT (~) flips all bits and left shift multiplies by 2.
✗ Incorrect
NOT 4 flips bits to -5 (in two's complement). Left shifting -5 by 1 multiplies by 2, resulting in -10.
❓ Predict Output
advancedRight Shift on Negative Number
What will this JavaScript code print?
const n = -16; const shifted = n >> 2; console.log(shifted);
DSA Javascript
const n = -16; const shifted = n >> 2; console.log(shifted);
Attempts:
2 left
💡 Hint
Right shift on negative numbers keeps the sign bit.
✗ Incorrect
Right shifting -16 by 2 divides by 4 rounding down, resulting in -4.
🧠 Conceptual
expertNumber of Set Bits After Multiple Bitwise Operations
Given the code below, how many bits are set to 1 in the final value of
res?let a = 29; // binary 00011101 let b = 23; // binary 00010111 let res = (a & b) ^ (~a | b);
DSA Javascript
let a = 29; // binary 00011101 let b = 23; // binary 00010111 let res = (a & b) ^ (~a | b);
Attempts:
2 left
💡 Hint
Calculate each part step by step in binary, then count 1s in the final result.
✗ Incorrect
a & b = 21 (00010101), ~a = -30 (two's complement), ~a | b = -6, XOR with 21 results in -27, which in 32-bit binary has 5 bits set to 1.
