Bird
Raised Fist0
DSA C#programming~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.
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.
Click to reveal answer
beginner
What does the NOT (~) operator do to a bit?
The NOT operator flips the bit: 0 becomes 1, and 1 becomes 0.
Example: ~0 = 1, ~1 = 0 (in single bit view).
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.
Each shift to the left multiplies the number by 2.
Example: 3 << 1 = 6 (binary 11 becomes 110).
Click to reveal answer
What is the result of 5 & 3 in binary? (5 = 0101, 3 = 0011)
A0111
B0001
C0010
D0100
Which operator flips all bits of a number?
AAND (&)
BOR (|)
CXOR (^)
DNOT (~)
What does the expression 6 ^ 3 evaluate to? (6 = 0110, 3 = 0011)
A0101
B0111
C0011
D0001
What is the effect of right shifting a number by 1 (>> 1)?
AFlips all bits
BMultiplies the number by 2
CDivides the number by 2
DSets all bits to zero
Which bitwise operator returns 1 only when exactly one bit is 1?
AXOR (^)
BAND (&)
COR (|)
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 left shift and right shift a binary number by 1 position.
    Imagine moving digits in a number left or right.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the bitwise AND operator (&) do when applied to two bits?
      easy
      A. Returns 0 only if both bits are 0
      B. Returns 1 if either bit is 1
      C. Flips the bits from 0 to 1 and 1 to 0
      D. Returns 1 only if both bits are 1

      Solution

      1. Step 1: Understand bitwise AND operation

        The AND operator compares each bit of two numbers and returns 1 only if both bits are 1.
      2. Step 2: Apply to example bits

        For bits 1 & 1, result is 1; for 1 & 0 or 0 & 1 or 0 & 0, result is 0.
      3. Final Answer:

        Returns 1 only if both bits are 1 -> Option D
      4. Quick Check:

        AND = 1 only if both bits 1 [OK]
      Hint: AND returns 1 only when both bits are 1 [OK]
      Common Mistakes:
      • Confusing AND with OR operator
      • Thinking AND flips bits
      • Assuming AND returns 1 if either bit is 1
      2. Which of the following is the correct syntax to perform a left shift by 2 on an integer variable x in C#?
      easy
      A. x >> 2;
      B. x << 2;
      C. x <<< 2;
      D. x >> 2 <<;

      Solution

      1. Step 1: Recall left shift operator syntax in C#

        The left shift operator is << and used as x << n to shift bits left by n places.
      2. Step 2: Identify correct syntax among options

        x << 2; uses x << 2; which is correct. x >> 2; is right shift, C uses invalid operator, D is invalid syntax.
      3. Final Answer:

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

        Left shift syntax is x << n [OK]
      Hint: Use << for left shift, >> for right shift [OK]
      Common Mistakes:
      • Using >>> which is invalid in C#
      • Confusing left shift with right shift
      • Adding extra symbols after shift operator
      3. What is the output of the following C# code?
      int a = 5; // binary 0101
      int b = 3; // binary 0011
      int c = a ^ b;
      Console.WriteLine(c);
      medium
      A. 2
      B. 7
      C. 6
      D. 8

      Solution

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

        5 in binary is 0101, 3 is 0011. XOR (^) returns 1 if bits differ, else 0.
      2. Step 2: Calculate XOR bit by bit

        0101 ^ 0011 = 0110 (binary), which is 6 in decimal.
      3. Final Answer:

        6 -> Option C
      4. Quick Check:

        5 ^ 3 = 6 [OK]
      Hint: XOR sets bits where inputs differ [OK]
      Common Mistakes:
      • Confusing XOR with OR or AND
      • Calculating decimal sum instead of XOR
      • Misreading binary values
      4. Identify the error in the following C# code snippet that attempts to invert bits of an integer num:
      int num = 12;
      int inverted = ~num();
      Console.WriteLine(inverted);
      medium
      A. Using parentheses after variable name num()
      B. Using bitwise NOT operator incorrectly
      C. Missing semicolon after int num = 12
      D. Console.WriteLine cannot print integers

      Solution

      1. Step 1: Check usage of variable num

        The code uses num() which looks like a function call but num is an integer variable, so parentheses are invalid.
      2. Step 2: Confirm bitwise NOT operator usage

        The bitwise NOT operator ~ is correctly used before the variable, so no error there.
      3. Final Answer:

        Using parentheses after variable name num() -> Option A
      4. Quick Check:

        Variables don't use () [OK]
      Hint: Variables are not functions; no () after variable names [OK]
      Common Mistakes:
      • Thinking ~num() is valid syntax
      • Assuming missing semicolon error
      • Believing Console.WriteLine can't print integers
      5. Given an integer n = 8, which expression correctly checks if n is a power of two using bit manipulation in C#?
      hard
      A. (n & (n - 1)) == 0
      B. (n | (n - 1)) == 0
      C. (n ^ (n - 1)) == 0
      D. (~n & (n - 1)) == 0

      Solution

      1. Step 1: Understand the power of two property

        A number is a power of two if it has exactly one bit set to 1 in binary.
      2. Step 2: Apply bitwise trick

        For such numbers, n & (n - 1) clears the lowest set bit. If result is 0, n was a power of two.
      3. Final Answer:

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

        Power of two check uses n & (n-1) == 0 [OK]
      Hint: Power of two if n & (n-1) equals zero [OK]
      Common Mistakes:
      • Using OR or XOR instead of AND
      • Forgetting to compare result to zero
      • Misunderstanding bitwise NOT usage
      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