Bird
Raised Fist0
Cprogramming~5 mins

Bitwise AND, OR, XOR in C - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the bitwise AND operator (&) do in C?
It compares each bit of two numbers and returns 1 only if both bits are 1; otherwise, it returns 0.
Click to reveal answer
beginner
Explain the bitwise OR operator (|) in simple terms.
It compares each bit of two numbers and returns 1 if at least one of the bits is 1; otherwise, it returns 0.
Click to reveal answer
beginner
What is the result of the bitwise XOR operator (^) when both bits are the same?
The result is 0 because XOR returns 1 only when the bits are different.
Click to reveal answer
beginner
How would you describe bitwise operators using a real-life example?
Think of two light switches (bits). AND means both switches must be ON to get light. OR means if either switch is ON, the light is ON. XOR means the light is ON only if exactly one switch is ON.
Click to reveal answer
beginner
What is the output of this C code snippet? int a = 5; // binary 0101 int b = 3; // binary 0011 int c = a & b;
The output value of c is 1 because 0101 & 0011 = 0001 in binary.
Click to reveal answer
What does the bitwise OR operator (|) return when both bits are 0?
A0
B1
CDepends on the other bits
DError
Which operator returns 1 only when bits are different?
AAND (&)
BNOT (~)
COR (|)
DXOR (^)
What is the result of 6 & 3 in binary? (6 = 0110, 3 = 0011)
A0010 (2)
B0111 (7)
C0101 (5)
D0001 (1)
If a = 4 (0100) and b = 5 (0101), what is a ^ b?
A0100 (4)
B0001 (1)
C0101 (5)
D0000 (0)
Which bitwise operator would you use to check if a specific bit is set?
AXOR (^)
BOR (|)
CAND (&)
DNOT (~)
Describe how bitwise AND, OR, and XOR operators work with examples.
Think about comparing bits one by one.
You got /4 concepts.
    Explain a real-life analogy to understand bitwise AND, OR, and XOR.
    Use everyday objects to explain bits.
    You got /4 concepts.

      Practice

      (1/5)
      1. Which bitwise operator in C returns 1 only if both corresponding bits of two numbers are 1?
      a & b means:
      easy
      A. Bitwise AND
      B. Bitwise OR
      C. Bitwise XOR
      D. Logical AND

      Solution

      1. Step 1: Understand bitwise AND operation

        Bitwise AND compares each bit of two numbers and returns 1 only if both bits are 1.
      2. Step 2: Compare with other operators

        Bitwise OR returns 1 if either bit is 1, XOR returns 1 if bits differ, and logical AND is different (works on boolean values).
      3. Final Answer:

        Bitwise AND -> Option A
      4. Quick Check:

        AND = & = Bitwise AND [OK]
      Hint: AND means both bits must be 1 to get 1 [OK]
      Common Mistakes:
      • Confusing bitwise AND with logical AND (&&)
      • Mixing AND with OR or XOR operators
      • Thinking bitwise AND returns 1 if either bit is 1
      2. Which of the following is the correct syntax to perform bitwise XOR on two integers a and b in C?
      easy
      A. a ^ b
      B. a && b
      C. a | b
      D. a & b

      Solution

      1. Step 1: Identify bitwise XOR operator

        In C, the caret symbol (^) is used for bitwise XOR operation.
      2. Step 2: Differentiate from other operators

        && is logical AND, | is bitwise OR, & is bitwise AND, so only ^ is XOR.
      3. Final Answer:

        a ^ b -> Option A
      4. Quick Check:

        ^ means XOR in C [OK]
      Hint: XOR uses ^ symbol in C [OK]
      Common Mistakes:
      • Using && instead of ^ for XOR
      • Confusing | (OR) with ^ (XOR)
      • Using & (AND) instead of ^ (XOR)
      3. What is the output of the following C code?
      int a = 6;  // binary 0110
      int b = 11; // binary 1011
      printf("%d", a & b);
      medium
      A. 3
      B. 15
      C. 7
      D. 2

      Solution

      1. Step 1: Convert numbers to binary

        6 in binary is 0110, 11 in binary is 1011.
      2. Step 2: Perform bitwise AND

        0110 & 1011 = 0010 (only bits where both are 1 remain 1).
      3. Step 3: Convert result back to decimal

        0010 in decimal is 2.
      4. Final Answer:

        2 -> Option D
      5. Quick Check:

        6 & 11 = 2 [OK]
      Hint: AND keeps bits set in both numbers only [OK]
      Common Mistakes:
      • Confusing AND with OR or XOR results
      • Not converting binary back to decimal correctly
      • Mixing up bit positions
      4. Identify the error in this C code snippet that tries to perform bitwise OR:
      int a = 5;
      int b = 9;
      int c = a | b;
      printf("%d", c);
      medium
      A. Missing semicolon after variable declarations
      B. Incorrect operator used for bitwise OR
      C. No error, code will print correct output
      D. printf format specifier should be %x for bitwise OR

      Solution

      1. Step 1: Check syntax correctness

        All lines have semicolons; operator | is correct for bitwise OR.
      2. Step 2: Verify printf usage

        %d is correct to print integer decimal value; no error present.
      3. Final Answer:

        No error, code will print correct output -> Option C
      4. Quick Check:

        Bitwise OR uses | and %d prints decimal [OK]
      Hint: Bitwise OR uses | and %d prints decimal [OK]
      Common Mistakes:
      • Thinking | is wrong operator for OR
      • Believing %x is mandatory for bitwise results
      • Missing semicolons (not the case here)
      5. Given two integers x = 12 and y = 10, which expression toggles the bits of x where y has bits set, using bitwise XOR?
      hard
      A. x | y
      B. x ^ y
      C. x & y
      D. ~x ^ y

      Solution

      1. Step 1: Understand toggling bits with XOR

        Bitwise XOR toggles bits where the second operand has 1s.
      2. Step 2: Analyze each option

        x & y keeps bits set in both; x | y sets bits set in either; ~x ^ y is unrelated toggling with complement.
      3. Final Answer:

        x ^ y -> Option B
      4. Quick Check:

        XOR toggles bits where second operand has 1 [OK]
      Hint: XOR toggles bits where second number has 1s [OK]
      Common Mistakes:
      • Using AND or OR instead of XOR for toggling
      • Confusing complement operator ~ with XOR
      • Not understanding toggling effect of XOR
      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