Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bitwise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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);
A3
B5
C7
D1
Attempts:
2 left
💡 Hint
Remember how AND (&) and OR (|) work bit by bit.
❓ Predict Output
intermediate
2:00remaining
Effect 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);
A2
B6
C14
D8
Attempts:
2 left
💡 Hint
XOR (^) sets bits where bits differ.
❓ Predict Output
advanced
2:00remaining
Understanding 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);
A-10
B-8
C10
D8
Attempts:
2 left
💡 Hint
Remember NOT (~) flips all bits and left shift multiplies by 2.
❓ Predict Output
advanced
2:00remaining
Right 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);
A-4
B-5
C3
D12
Attempts:
2 left
💡 Hint
Right shift on negative numbers keeps the sign bit.
🧠 Conceptual
expert
3:00remaining
Number 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);
A4
B6
C3
D5
Attempts:
2 left
💡 Hint
Calculate each part step by step in binary, then count 1s in the final result.

Practice

(1/5)
1. What is the result of the bitwise AND operation between 5 and 3? (5 & 3)
easy
A. 1
B. 7
C. 0
D. 8

Solution

  1. Step 1: Convert numbers to binary

    5 in binary is 0101, 3 in binary is 0011.
  2. Step 2: Perform bitwise AND

    0101 & 0011 = 0001 (only bits set in both numbers remain set).
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    5 & 3 = 1 [OK]
Hint: AND keeps bits set in both numbers only [OK]
Common Mistakes:
  • Confusing AND with OR operation
  • Not converting numbers to binary before operation
  • Assuming decimal addition instead of bitwise
2. Which of the following is the correct syntax for performing a bitwise XOR operation between variables a and b in JavaScript?
easy
A. let result = a | b;
B. let result = a && b;
C. let result = a % b;
D. let result = a ^ b;

Solution

  1. Step 1: Identify bitwise XOR operator in JavaScript

    The XOR operator is represented by the caret symbol (^).
  2. Step 2: Check syntax correctness

    let result = a ^ b; uses a ^ b, which is the correct syntax for bitwise XOR.
  3. Final Answer:

    let result = a ^ b; -> Option D
  4. Quick Check:

    XOR operator is ^ in JavaScript [OK]
Hint: XOR uses ^ symbol in JavaScript [OK]
Common Mistakes:
  • Using && which is logical AND, not bitwise
  • Using | which is bitwise OR, not XOR
  • Using % which is modulo, unrelated to bits
3. What will be the output of the following JavaScript code?
let x = 8; // binary 1000
let y = x >> 2;
console.log(y);
medium
A. 2
B. 4
C. 1
D. 0

Solution

  1. Step 1: Understand right shift operation

    Right shift by 2 means move bits 2 places to the right, dropping the rightmost bits.
  2. Step 2: Apply right shift on binary 1000

    1000 >> 2 becomes 0010, which is decimal 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    8 >> 2 = 2 [OK]
Hint: Right shift divides by 2 for each shift [OK]
Common Mistakes:
  • Confusing right shift with left shift
  • Not converting binary to decimal after shift
  • Assuming shift adds zeros on left without dropping bits
4. Find the error in the following code snippet that attempts to flip all bits of a number n using bitwise NOT:
let n = 5;
let flipped = ~n;
console.log(flipped);

What is the issue?
medium
A. Missing parentheses around ~n
B. Bitwise NOT returns a negative number due to two's complement representation
C. Variable n must be a string for bitwise NOT
D. Bitwise NOT operator (~) is not supported in JavaScript

Solution

  1. Step 1: Understand bitwise NOT in JavaScript

    Bitwise NOT flips all bits and returns the two's complement negative number.
  2. Step 2: Explain why output is negative

    For 5 (00000101), ~5 flips bits to 11111010 which is -6 in two's complement.
  3. Final Answer:

    Bitwise NOT returns a negative number due to two's complement representation -> Option B
  4. Quick Check:

    ~n flips bits and returns negative number [OK]
Hint: Bitwise NOT flips bits and returns negative in JS [OK]
Common Mistakes:
  • Expecting positive flipped bits as output
  • Thinking ~ is unsupported operator
  • Assuming input must be string for bitwise ops
5. You want to set the 3rd bit (0-indexed from right) of a number num to 1 without changing other bits. Which expression correctly does this in JavaScript?
hard
A. num = num ^ (1 << 3);
B. num = num & (1 << 3);
C. num = num | (1 << 3);
D. num = num << 3;

Solution

  1. Step 1: Understand setting a bit using OR and shift

    Left shift 1 by 3 gives 00001000, which targets the 3rd bit.
  2. Step 2: Use OR to set the bit without changing others

    OR with this mask sets the 3rd bit to 1, keeping others unchanged.
  3. Final Answer:

    num = num | (1 << 3); -> Option C
  4. Quick Check:

    Use OR with shifted 1 to set bit [OK]
Hint: Use OR with (1 << bit_position) to set bit [OK]
Common Mistakes:
  • Using AND which clears bits instead of setting
  • Using XOR which toggles bits and may unset
  • Using left shift alone which changes number value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard