Bird
Raised Fist0
DSA Cprogramming~5 mins

Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA C - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the AND (&) bitwise operator do between two bits?
The AND operator returns 1 only if both bits are 1; otherwise, it returns 0.
Example: 1 & 1 = 1, 1 & 0 = 0, 0 & 0 = 0.
Click to reveal answer
beginner
Explain the OR (|) bitwise operator with an example.
The OR operator returns 1 if at least one of the bits is 1; otherwise, it returns 0.
Example: 1 | 0 = 1, 0 | 0 = 0, 1 | 1 = 1.
Click to reveal answer
beginner
What is the result of XOR (^) between two bits?
XOR returns 1 if the bits are different, and 0 if they are the same.
Example: 1 ^ 0 = 1, 1 ^ 1 = 0, 0 ^ 0 = 0.
Click to reveal answer
beginner
What does the NOT (~) operator do to a bit?
The NOT operator flips the bit: 1 becomes 0, and 0 becomes 1.
Example: ~1 = 0, ~0 = 1 (in binary representation).
Click to reveal answer
beginner
What happens when you perform a left shift (<<) on a number?
Left shift moves all bits to the left by the given number of positions, adding zeros on the right.
This is like multiplying the number by 2 for each shift.
Example: 3 (011) << 1 = 6 (110).
Click to reveal answer
What is the result of 5 & 3 in binary? (5 = 0101, 3 = 0011)
A0100
B0111
C0001
D0010
Which operator flips all bits of a number?
AOR (|)
BNOT (~)
CAND (&)
DXOR (^)
What does the expression (6 >> 1) evaluate to? (6 = 0110 in binary)
A0001 (1)
B1100 (12)
C0110 (6)
D0011 (3)
What is the result of 4 ^ 4?
A0
B4
C8
D1
Which bitwise operator returns 1 if either bit is 1?
AOR (|)
BXOR (^)
CAND (&)
DNOT (~)
Describe how the AND, OR, XOR, and NOT bitwise operators work with simple examples.
Think about how each operator treats pairs of bits.
You got /4 concepts.
    Explain what happens when you perform left shift (<<) and right shift (>>) on a binary number.
    Imagine sliding bits left or right in a row.
    You got /4 concepts.

      Practice

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

      Solution

      1. Step 1: Convert numbers to binary

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

        0101 AND 0011 = 0001 (only bits set in both numbers remain set).
      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
      • Mixing bit positions during calculation
      2. Which of the following is the correct syntax for left shifting the integer x by 2 bits 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

        The left shift operator is written as << and shifts bits to the left.
      2. Step 2: Check syntax correctness

        x << 2; uses x << 2; which correctly shifts bits of x left by 2 positions.
      3. Final Answer:

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

        Left shift syntax = x << 2; [OK]
      Hint: Use << for left shift, >> for right shift [OK]
      Common Mistakes:
      • Using >>> which is invalid in C
      • Confusing left shift with right shift
      • Incorrect operator placement or syntax
      3. What is the output of the following C code?
      int x = 6; // binary 0110
      int y = x ^ 3; // 3 = 0011
      printf("%d", y);
      medium
      A. 9
      B. 7
      C. 1
      D. 5

      Solution

      1. Step 1: Convert numbers to binary and apply XOR

        6 in binary is 0110, 3 is 0011. XOR operation: 0110 ^ 0011 = 0101.
      2. Step 2: Calculate decimal value of XOR result

        0101 in binary equals 5 decimal.
      3. Final Answer:

        5 -> Option D
      4. Quick Check:

        6 ^ 3 = 5 [OK]
      Hint: XOR flips bits where bits differ [OK]
      Common Mistakes:
      • Mixing XOR with OR or AND
      • Incorrect binary conversion
      • Misreading bit positions
      4. Identify the error in the following C code snippet:
      int x = 8;
      int y = ~x << 1;
      printf("%d", y);
      medium
      A. Using ~ operator without parentheses causes wrong shift
      B. Missing semicolon after declaration
      C. Shift operator << cannot be used with ~ operator
      D. No error, code runs correctly

      Solution

      1. Step 1: Understand operator precedence

        The bitwise NOT (~) has higher precedence than shift (<<), so ~x is evaluated first.
      2. Step 2: Check if parentheses needed

        Without parentheses, ~x is computed, then shifted left. This is correct but may cause confusion if intended differently.
      3. Step 3: Identify common mistake

        If intention was to shift x first then NOT, parentheses are needed. Otherwise, no syntax error but logic may be wrong.
      4. Final Answer:

        Using ~ operator without parentheses causes wrong shift -> Option A
      5. Quick Check:

        Operator precedence matters; use parentheses [OK]
      Hint: Use parentheses to control bitwise operation order [OK]
      Common Mistakes:
      • Ignoring operator precedence
      • Assuming shift applies before NOT
      • Thinking code has syntax error when it doesn't
      5. Given an integer n, which expression correctly checks if the 3rd bit (from right, zero-based) is set (1)?
      hard
      A. (n & (1 << 3)) != 0
      B. (n | (1 << 3)) == 0
      C. (n ^ (1 << 3)) == 0
      D. (n >> 3) & 1 == 0

      Solution

      1. Step 1: Understand bit position and mask

        The 3rd bit (zero-based) means bit at position 3. Mask is 1 shifted left by 3: (1 << 3) = 8.
      2. Step 2: Check if bit is set using AND

        Using (n & (1 << 3)) checks if that bit is 1. If result is not zero, bit is set.
      3. Final Answer:

        (n & (1 << 3)) != 0 -> Option A
      4. Quick Check:

        Bit check uses AND with mask [OK]
      Hint: Use AND with (1 << bit_position) to check bit [OK]
      Common Mistakes:
      • Using OR or XOR instead of AND for checking
      • Wrong bit position shift
      • Comparing shifted bit to zero incorrectly
      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