Check Even/Odd Using Bitwise AND
C++
Medium
3 views
Problem Description
Use bitwise AND with 1 to check even/odd instantly.
Real Life: Fastest way to check even/odd in programming.
Step-by-Step Logic:
1. Last bit of even numbers is 0
2. Last bit of odd numbers is 1
3. num & 1 gives last bit
4. If result is 0, number is even
Official Solution
void operator_q9_even_odd_bitwise() {
int numbers[] = {12, 15, 18, 21, 24};
for(int i = 0; i < 5; i++) {
if(numbers[i] & 1) {
cout << numbers[i] << " is Odd" << endl;
} else {
cout << numbers[i] << " is Even" << endl;
}
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!