Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bitwise Mastery Badge
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 C++ code snippet?
DSA C++
int a = 12;  // binary: 1100
int b = 10;  // binary: 1010
int c = a & b;
int d = a | b;
std::cout << c << " " << d << std::endl;
A8 14
B4 14
C8 12
D4 12
Attempts:
2 left
💡 Hint
Recall that & compares bits and returns 1 only if both bits are 1; | returns 1 if either bit is 1.
❓ Predict Output
intermediate
2:00remaining
Result of XOR and NOT Operations
What will be printed by this C++ code?
DSA C++
unsigned int x = 5;  // binary: 0000 0101
unsigned int y = 9;  // binary: 0000 1001
unsigned int z = x ^ y;
unsigned int w = ~x;
std::cout << z << " " << w << std::endl;
A12 4294967290
B12 250
C13 4294967290
D13 250
Attempts:
2 left
💡 Hint
XOR returns 1 if bits differ; NOT flips all bits. Remember unsigned int is 32-bit.
❓ Predict Output
advanced
2:00remaining
Left and Right Shift Effects on Signed Integers
What is the output of this C++ code snippet?
DSA C++
int a = -8;  // binary (two's complement): 1111 1000
int b = a >> 2;
int c = a << 2;
std::cout << b << " " << c << std::endl;
A2 32
B-2 32
C-2 -32
D2 -32
Attempts:
2 left
💡 Hint
Right shift on signed int usually preserves sign (arithmetic shift). Left shift multiplies by powers of two.
🧠 Conceptual
advanced
1:30remaining
Understanding Bitwise NOT on Signed Integers
What is the decimal value of ~(-1) in a 32-bit signed integer system?
A-1
B1
C4294967294
D0
Attempts:
2 left
💡 Hint
Remember that -1 in two's complement is all bits set to 1.
❓ Predict Output
expert
2:30remaining
Complex Bitwise Expression Evaluation
What is the output of this C++ code?
DSA C++
unsigned int x = 29;  // binary: 0001 1101
unsigned int y = 15;  // binary: 0000 1111
unsigned int z = ((x & y) << 2) ^ (~y >> 1);
std::cout << z << std::endl;
A4294967274
B2147483596
C4294967282
D4294967266
Attempts:
2 left
💡 Hint
Break down the expression step-by-step: AND, shifts, NOT, then XOR.

Practice

(1/5)
1. What is the result of the bitwise AND operation between 5 and 3? (In binary, 5 = 0101, 3 = 0011)
easy
A. 0
B. 1
C. 6
D. 7

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 AND 0011 = 0001 (only bits where both are 1 remain 1).
  3. Final Answer:

    1 -> Option B
  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 decimal to binary correctly
  • Assuming bitwise AND adds numbers
2. Which of the following is the correct syntax for performing a left shift by 2 bits on an integer variable x in C++?
easy
A. x << 2;
B. x >> 2;
C. x <<< 2;
D. x >> 2 <<;

Solution

  1. Step 1: Understand left shift operator

    In C++, left shift is done using << operator.
  2. Step 2: Check syntax correctness

    x << 2; uses x << 2; which is correct syntax for left shift by 2 bits.
  3. Final Answer:

    x << 2; -> Option A
  4. Quick Check:

    Left shift uses << operator [OK]
Hint: Left shift uses <<, right shift uses >> [OK]
Common Mistakes:
  • Using >>> which is not valid in C++
  • Confusing left shift with right shift
  • Adding extra symbols after operator
3. What is the output of the following C++ code?
int x = 12;  // binary 1100
int y = 5;   // binary 0101
int z = x ^ y;
std::cout << z << std::endl;
medium
A. 8
B. 15
C. 9
D. 7

Solution

  1. Step 1: Convert numbers to binary

    x = 12 (1100), y = 5 (0101).
  2. Step 2: Perform bitwise XOR

    1100 XOR 0101 = 1001 (bits differ become 1).
  3. Step 3: Convert result back to decimal

    1001 in decimal is 9.
  4. Final Answer:

    9 -> Option C
  5. Quick Check:

    12 ^ 5 = 9 [OK]
Hint: XOR sets bits where inputs differ [OK]
Common Mistakes:
  • Mixing XOR with OR or AND
  • Not converting binary to decimal correctly
  • Assuming XOR adds numbers
4. Identify the error in the following C++ code snippet:
int a = 8;
int b = a >> 2;
int c = ~b;
std::cout << c << std::endl;
medium
A. No error; code runs and prints the result
B. Using bitwise NOT (~) on an integer is invalid
C. Right shift operator (>>) is used incorrectly
D. Missing semicolon after variable declaration

Solution

  1. Step 1: Check each operation

    Right shift (>>) on integer 8 by 2 bits is valid: 8 (1000) >> 2 = 2 (0010).
  2. Step 2: Check bitwise NOT usage

    Bitwise NOT (~) on integer 2 flips bits; this is valid in C++.
  3. Step 3: Verify syntax

    All semicolons are present; code compiles and runs.
  4. Final Answer:

    No error; code runs and prints the result -> Option A
  5. Quick Check:

    Bitwise NOT and shifts are valid [OK]
Hint: Bitwise NOT and shifts are valid on integers [OK]
Common Mistakes:
  • Thinking ~ is invalid on integers
  • Confusing shift direction
  • Assuming missing semicolon without checking
5. Given an integer n = 29, which of the following expressions correctly clears (sets to 0) the 3rd bit (counting from 0, right to left) using bit manipulation?
hard
A. ~n & (1 << 3)
B. n | (1 << 3)
C. n ^ (1 << 3)
D. n & ~(1 << 3)

Solution

  1. Step 1: Understand clearing a bit

    To clear a bit, use AND with NOT mask that has 1 at target bit.
  2. Step 2: Analyze each option

    n & ~(1 << 3): n & ~(1 << 3) clears 3rd bit correctly.
    n | (1 << 3): n | (1 << 3) sets 3rd bit.
    n ^ (1 << 3): n ^ (1 << 3) toggles 3rd bit.
    ~n & (1 << 3): ~n & (1 << 3) isolates 3rd bit of inverted n, not clearing.
  3. Final Answer:

    n & ~(1 << 3) -> Option D
  4. Quick Check:

    Clear bit = AND with NOT mask [OK]
Hint: Clear bit: AND with NOT of bit mask [OK]
Common Mistakes:
  • Using OR instead of AND to clear bits
  • Confusing toggle (XOR) with clear
  • Misunderstanding bit indexing
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