📖 Scenario: You are building a simple program that checks if a number is even or odd using bit operations. This is like checking if a number is divisible by 2 without using division or modulus. It is a fast way computers decide even or odd.
🎯 Goal: Create a program that uses bitwise AND to check if a number is even or odd and prints the result.
📋 What You'll Learn
Create an integer variable named number with the value 29
Create an integer variable named check to hold the bitwise AND result
Use bitwise AND operator & with number and 1 to set check
Print "Even" if check is 0, otherwise print "Odd"
💡 Why This Matters
🌍 Real World
Checking even or odd numbers quickly is useful in many programs like games, data processing, and hardware control.
💼 Career
Understanding bitwise operations is important for low-level programming, embedded systems, and performance optimization.
Progress0 / 4 steps
1
Create the number variable
Create an integer variable called number and set it to 29.
DSA C
Hint
Use int number = 29; to create the variable.
2
Create the check variable
Create an integer variable called check to store the bitwise AND result.
DSA C
Hint
Declare int check; without initializing it yet.
3
Use bitwise AND to check even or odd
Set check to the result of number & 1 using the bitwise AND operator.
DSA C
Hint
Use check = number & 1; to get the last bit.
4
Print if number is Even or Odd
Write an if statement to print "Even" if check is 0, else print "Odd".
DSA C
Hint
Use if (check == 0) to check even, else print odd.
Practice
(1/5)
1. Which bit operation can be used to check if a number is even or odd?
easy
A. Shift the number right by 1
B. Check the least significant bit using AND with 1
C. XOR the number with 1
D. Invert all bits of the number
Solution
Step 1: Understand the least significant bit (LSB)
The LSB of a binary number determines if it is even or odd: 0 means even, 1 means odd.
Step 2: Use AND operation with 1 to isolate LSB
Performing bitwise AND with 1 keeps only the LSB. If result is 0, number is even; if 1, odd.
Final Answer:
Check the least significant bit using AND with 1 -> Option B
Quick Check:
LSB & 1 = 0 (even) or 1 (odd) [OK]
Hint: Use n & 1 to check even or odd quickly [OK]
Common Mistakes:
Using XOR instead of AND to check LSB
Checking the most significant bit instead
Using bit shift without checking bits
2. Which of the following C code snippets correctly checks if an integer n is odd using bitwise operators?
easy
A. if ((n & 1) == 1) printf("Odd");
B. if ((n >> 1) == 1) printf("Odd");
C. if ((n & 1) == 0) printf("Odd");
D. if ((n ^ 1) == 1) printf("Odd");
Solution
Step 1: Understand bitwise AND with 1
Using n & 1 isolates the least significant bit. If it equals 1, the number is odd.
Step 2: Check condition for odd number
The correct condition is (n & 1) == 1 to print "Odd".
Final Answer:
if ((n & 1) == 1) printf("Odd"); -> Option A
Quick Check:
n & 1 == 1 means odd [OK]
Hint: Use (n & 1) == 1 to detect odd numbers [OK]
Common Mistakes:
Checking for == 0 to print odd (actually even)
Using right shift instead of AND
Using XOR incorrectly for odd check
3. What is the output of the following C code?
int n = 14;
if (n & 1)
printf("Odd");
else
printf("Even");
medium
A. Odd
B. Compilation error
C. Even
D. 14
Solution
Step 1: Evaluate n & 1 for n = 14
Binary of 14 is 1110. The least significant bit is 0. So, 14 & 1 = 0.
Step 2: Check the if condition
Since (n & 1) is 0 (false), the else block executes, printing "Even".
Final Answer:
Even -> Option C
Quick Check:
14 is even, so output is Even [OK]
Hint: Check LSB: 0 means even, 1 means odd [OK]
Common Mistakes:
Assuming 14 is odd because it ends with 4
Confusing bitwise AND with logical AND
Expecting output "14" instead of even/odd
4. Identify the error in this C code snippet that checks if a number is odd:
int n = 7;
if (n & 1 == 1)
printf("Odd");
else
printf("Even");
medium
A. Missing parentheses around (n & 1)
B. Using == instead of = in if condition
C. Incorrect use of bitwise OR instead of AND
D. No error, code works fine
Solution
Step 1: Analyze operator precedence in condition
In C, == has higher precedence than &. So expression is evaluated as n & (1 == 1).
Step 2: Evaluate the expression and fix
Since (1 == 1) is true (1), expression becomes n & 1, which is correct but confusing. To avoid this, parentheses should be used: (n & 1) == 1.
Final Answer:
Missing parentheses around (n & 1) -> Option A
Quick Check:
Use (n & 1) == 1 to ensure correct evaluation [OK]
Hint: Use parentheses: (n & 1) == 1 to avoid precedence errors [OK]
5. You want to write a function in C that returns 1 if a number is odd and 0 if even, using bitwise operators. Which of the following implementations is correct and efficient?
int isOdd(int n) {
// Choose one
}
hard
A. return (n >> 1) & 1;
B. return !(n & 1);
C. return n ^ 1;
D. return (n & 1) == 1 ? 1 : 0;
Solution
Step 1: Understand the goal of the function
The function should return 1 if n is odd, 0 if even, using bitwise operations.
Step 2: Evaluate each option
return (n & 1) == 1 ? 1 : 0; correctly returns 1 if LSB is 1 (odd), else 0. return (n >> 1) & 1; returns the second least significant bit, incorrect. return n ^ 1; returns n XOR 1, which changes the number, not a boolean. return !(n & 1); returns 1 if even, opposite of requirement.
Final Answer:
return (n & 1) == 1 ? 1 : 0; -> Option D
Quick Check:
Use (n & 1) == 1 to detect odd and return 1 [OK]
Hint: Return (n & 1) == 1 for odd check function [OK]
Common Mistakes:
Using bit shift instead of checking LSB
Returning modified number instead of boolean
Negating the condition incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.