Find If a Number is Even or Odd Using Bitwise Operator.
Created On: 14 June 2025
Understanding whether a number is even or odd is one of the most common tasks in programming. While most beginners use the arithmetic modulus operator (%), there’s a faster and more efficient alternative the bitwise AND operator (&).
In this post, we'll explore both approaches, compare them, and understand why bitwise is better.
Approach 1: Using Arithmetic Operator.
The most common and straightforward method is using the modulus operator (%). If a number divided by 2 leaves a remainder of 0, it's even. Otherwise, it's odd.
if (n % 2 == 0) → Even
else→ Odd
Example Code:
// C++ Example: Find Number is Odd or Even#include <iostream>usingnamespace std;
void checkEvenOrOdd_Arithmetic(int n) {
if (n % 2 == 0)
cout << n << " is Even" << endl;
else
cout << n << " is Odd" << endl;
}
# Python Example: Find Number is Even or Odddef check_even_or_odd_arithmetic(n):
if n % 2 == 0:
print(f"{n} is Even")
else:
print(f"{n} is Odd")
// Java Example: Find Number is Even or Oddpublicclass CheckEvenOrOdd {
publicstaticvoid checkEvenOrOddArithmetic(int n) {
if (n % 2 == 0) {
System.out.println(n + " is Even");
} else {
System.out.println(n + " is Odd");
}
}
publicstaticvoid main(String[] args) {
// Example usageint number = 10;
checkEvenOrOddArithmetic(number);
}
}
Output:
6 is Even
Approach 2: Using Bitwise AND Operator.
A faster and smarter way to check if a number is even or odd is using bitwise operators.
The & operator performs a bit-by-bit comparison between two numbers.
For each bit, it returns:
1 if both bits are 1
0 otherwise
When you perform n & 1, you're checking the Least Significant Bit (LSB) of the number.
If LSB is 0 → number is even
If LSB is 1 → number is odd
Because the binary of even number always end with 0 and binary of odd number always end with 1. Let's understand this with few examples:
Example 1:
Number = 4
Binary of 4: 0100
Binary of 1: 00010100
AND 0001
= 0000→0→ Even
Example 2:
Number = 7
Binary of 7: 0111
Binary of 1: 00010111
AND 0001
= 0001→1→ Odd
Example Code:
//C++ Example: Find Even and Odd Using Bitwise AND#include <iostream>usingnamespace std;
void checkEvenOrOdd_Bitwise(int n) {
if (n & 1)
cout << n << " is Odd" << endl;
else
cout << n << " is Even" << endl;
}
int main() {
checkEvenOrOdd_Bitwise(4);
return0;
}
#Python Example: Find Even and Odd Using def check_even_or_odd_bitwise(n):if n & 1:
print(f"{n} is Odd")
else:
print(f"{n} is Even")
# Example usageif __name__ == "__main__":
check_even_or_odd_bitwise(4)
//Java Example: Find Even and Odd Using Bitwise ANDpublicclass CheckEvenOrOddBitwise {
publicstaticvoid checkEvenOrOddBitwise(int n) {
if ((n & 1) == 1) {
System.out.println(n + " is Odd");
} else {
System.out.println(n + " is Even");
}
}
publicstaticvoid main(String[] args) {
checkEvenOrOddBitwise(4);
}
}
Output:
6 is Even
Using n & 1 is a fast and efficient way to check if a number is even or odd by examining its last binary bit. It's a foundational concept in bit manipulation that helps build strong binary thinking.
⚡ Please share your valuable feedback and suggestion in the
comment section below or you can send us an email on our offical
email id ✉
algolesson@gmail.com. You can also support our
work by buying a cup of coffee ☕ for us.
No comments:
Post a Comment