Bit Manipulation Basics AND OR XOR NOT Left Right Shift
📖 Scenario: You are working with a simple device that stores settings as bits in a number. Each bit can be turned on or off to control different features.Understanding how to change these bits using basic operations like AND, OR, XOR, NOT, and shifting bits left or right is important to control the device.
🎯 Goal: You will create a program that starts with a number, then uses bit manipulation operations to change its bits step-by-step. Finally, you will print the results after each operation to see how the bits change.
📋 What You'll Learn
Create an integer variable with a specific starting value.
Create a mask variable for bit operations.
Use AND, OR, XOR, NOT, left shift, and right shift operations on the integer.
Print the results after each operation.
💡 Why This Matters
🌍 Real World
Bit manipulation is used in low-level programming, device control, and performance optimization where direct control of bits is needed.
💼 Career
Understanding bitwise operations is important for roles in embedded systems, game development, network programming, and systems programming.
Progress0 / 4 steps
1
Create the initial number
Create an integer variable called number and set it to 29.
DSA C#
Hint
Use int number = 29; to create the variable.
2
Create a mask for bit operations
Create an integer variable called mask and set it to 14.
DSA C#
Hint
Use int mask = 14; to create the mask variable.
3
Apply bitwise AND, OR, XOR, NOT, left shift, and right shift
Create six new integer variables: andResult, orResult, xorResult, notResult, leftShiftResult, and rightShiftResult. Set them to the results of applying bitwise AND, OR, XOR between number and mask, bitwise NOT of number, left shift number by 2, and right shift number by 2, respectively.
DSA C#
Hint
Use & for AND, | for OR, ^ for XOR, ~ for NOT, << for left shift, and >> for right shift.
4
Print the results of bit operations
Print the values of andResult, orResult, xorResult, notResult, leftShiftResult, and rightShiftResult each on a new line.
DSA C#
Hint
Use System.Console.WriteLine() to print each variable on its own line.
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
Step 1: Understand bitwise AND operation
The AND operator compares each bit of two numbers and returns 1 only if both bits are 1.
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.
Final Answer:
Returns 1 only if both bits are 1 -> Option D
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
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.
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.
Final Answer:
x << 2; -> Option B
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
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.
Step 2: Calculate XOR bit by bit
0101 ^ 0011 = 0110 (binary), which is 6 in decimal.
Final Answer:
6 -> Option C
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
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.
Step 2: Confirm bitwise NOT operator usage
The bitwise NOT operator ~ is correctly used before the variable, so no error there.
Final Answer:
Using parentheses after variable name num() -> Option A
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
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.
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.
Final Answer:
(n & (n - 1)) == 0 -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.