
Introduction
One of the very first problems programmers learn is determining whether a number is even or odd. At first glance, this may appear to be a simple mathematical problem that can easily be solved using the modulus operator.
For example:
if (n % 2 == 0)
However, experienced programmers and system-level developers often use a different approach known as bit manipulation.
Instead of division or modulus operations, they use:
n & 1
This technique is faster, more efficient, and closer to how computers actually process data internally.
In this article, we will deeply explore:
Binary number systems
Least Significant Bit (LSB)
Bitwise operators
Why
n & 1worksStep-by-step examples
C implementation
Performance analysis
Real-world applications
Interview insights
By the end, you will fully understand not only how the technique works, but also why it works.
Understanding Even and Odd Numbers
Before diving into bit manipulation, let us first understand the mathematical definition.
Even Numbers
A number is called even if it is completely divisible by 2.
Examples:
2, 4, 6, 8, 10, 12
Mathematically:
n % 2 == 0
Odd Numbers
A number is called odd if it leaves a remainder of 1 when divided by 2.
Examples:
1, 3, 5, 7, 9, 11
Mathematically:
n % 2 != 0
This is the traditional approach. But computers internally work differently.
How Computers Store Numbers
Computers do not store numbers in decimal format.
They store everything in binary.
Binary uses only two digits:
01
These digits are called bits.
Decimal to Binary Examples
DecimalBinary11210311410051016110711181000
Now observe carefully.
The Important Observation
Look at the last digit of each binary number.
DecimalBinaryLast Bit2100410006110081000031115101171111
Pattern
Even numbers always end with
0Odd numbers always end with
1
This is the key idea behind the entire technique.
Least Significant Bit (LSB)
The rightmost bit in a binary number is called the:
Least Significant Bit
or simply:
LSB
Examples:
NumberBinaryLSB101010013110111810010025110011
The LSB alone determines whether the number is even or odd.
Rule
LSB = 0 → Even
LSB = 1 → Odd
Now the question becomes:
How do we check the last bit efficiently?
The answer is bitwise operations.
Introduction to Bitwise AND Operator
The bitwise AND operator is represented using:
&
It compares bits position by position.
Truth Table of AND
ABA & B000010100111
The result becomes 1 only when both bits are 1.
The Main Trick
We use:
n & 1
Why does this work?
Because:
1 = 0001
When we AND any number with 1, all bits except the last bit become irrelevant.
Only the least significant bit remains.
Example 1 — Even Number
Let us take:
n = 10
Binary representation:
10 = 1010
1 = 0001
--------------
0000
Result:
10 & 1 = 0
Since the result is 0, the number is even.
Example 2 — Odd Number
Now take:
n = 13
Binary representation:
13 = 1101
1 = 0001
--------------
0001
Result:
13 & 1 = 1
Since the result is 1, the number is odd.
Why This Technique is Efficient
Traditional method:
n % 2
Bitwise method:
n & 1
Bitwise operations are:
Faster
Simpler at hardware level
Efficient in low-level systems
Frequently optimized by CPUs
Processors are designed to perform binary operations extremely quickly.
Step-by-Step Algorithm
Algorithm
Step 1
Take integer input n
Step 2
Perform:
n & 1
Step 3
Check result:
0→ Even1→ Odd
C Program Implementation
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n & 1)
printf("%d is Odd\n", n);
else
printf("%d is Even\n", n);
return 0;
}
Code Explanation
Header File
#include <stdio.h>
Used for input and output functions.
Variable Declaration
int n;
Stores the user input.
Input
scanf("%d", &n);
Reads an integer from the user.
Core Logic
if (n & 1)
Checks whether the least significant bit is 1.
Output
If true:
Odd
Else:
Even
Dry Run of the Program
Suppose input:
25
Binary form:
25 = 11001
Operation:
11001
00001
-----
00001
Result:
1
Therefore:
25 is Odd
Time Complexity Analysis
Time Complexity
O(1)
Because bitwise AND is a constant-time operation.
Space Complexity
O(1)
No extra memory is required.
Real-World Applications
Although this example seems basic, the underlying concept is extremely important.
1. Competitive Programming
Bit manipulation is heavily used to optimize solutions.
Checking parity using bits is faster than division.
2. Embedded Systems
Embedded devices have limited processing power.
Efficient operations are critical.
3. Operating Systems
Operating systems use bit operations for:
Process management
Permissions
Memory allocation
4. Cryptography
Encryption algorithms heavily rely on binary operations.
5. Game Engines
Games use bit manipulation for:
Collision systems
Physics calculations
State management
Common Interview Question
Interviewers often ask:
Why does n & 1 work?
Correct Explanation
Because the least significant bit determines parity.
0means even1means odd
ANDing with 1 isolates the last bit.
Common Mistakes
Mistake 1
Confusing logical AND with bitwise AND.
Wrong:
n && 1
Correct:
n & 1
Mistake 2
Not understanding binary representation.
Bit manipulation becomes difficult without understanding binary numbers.
Comparison Table
MethodExpressionEfficiencyTraditionaln % 2GoodBitwisen & 1Better
Key Takeaways
Computers store numbers in binary.
The last bit determines parity.
Even numbers end with
0.Odd numbers end with
1.n & 1isolates the least significant bit.Bitwise operations are fast and efficient.
This concept forms the foundation for advanced bit manipulation.
Conclusion
Checking whether a number is even or odd using bit manipulation is one of the simplest and most elegant examples of low-level programming optimization.
Although the traditional modulus operator works perfectly well, bitwise techniques provide deeper insight into how computers operate internally.
Learning such techniques strengthens your understanding of:
Binary arithmetic
CPU-level operations
Performance optimization
Systems programming
Most importantly, this topic serves as a gateway to advanced concepts like:
Bitmasking
Set and clear operations
XOR tricks
State compression
Competitive programming optimizations
Mastering small concepts like this builds the foundation for becoming an efficient and skilled programmer.
