Check if a Number is Even/Odd - Visualization

Visualization Player

Problem Statement

Given an integer n, determine whether the number is even or odd using bit manipulation.

This is a fast and clever alternative to using modulo (%) operations.

Key Insight: The least significant bit (LSB) of a binary number determines its parity. If the LSB is 0, the number is even. If the LSB is 1, the number is odd.

Examples

n Output Description
0 even 0 is even; LSB is 0 (binary: 0)
1 odd 1 is odd; LSB is 1 (binary: 1)
2 even 2 is even; LSB is 0 (binary: 10)
15 odd 15 is odd; LSB is 1 (binary: 1111)
16 even 16 is even; LSB is 0 (binary: 10000)
-1 odd -1 is odd; LSB is 1 (two's complement binary: ...1111)
-2 even -2 is even; LSB is 0 (two's complement binary: ...1110)
1023 odd 1023 is odd; binary: 1111111111, LSB is 1
1024 even 1024 is even; binary: 10000000000, LSB is 0
"" even Empty input treated as 0, which is even

Solution

Understanding the Problem

We are given an integer n, and our goal is to check whether this number is even or odd. Normally, this is done using the modulo operator (n % 2), but in this case, we want to solve it using bitwise operations.

This approach is fast and memory-efficient because bitwise operations work directly at the binary level. The key insight here is:

  • In binary, the least significant bit (LSB) of a number determines whether it's even or odd.
  • If the LSB is 0, the number is even.
  • If the LSB is 1, the number is odd.

We can extract the LSB using the bitwise AND operation: n & 1.

Step-by-Step Solution with Example

step 1: Take an example number

Let's consider n = 10. In binary, this is 1010.

step 2: Perform bitwise AND with 1

We apply the operation: 10 & 1.

Binary of 10 is 1010

Binary of 1 is 0001

Bitwise AND result: 0000 → which is 0.

step 3: Interpret the result

Since the result is 0, that means the least significant bit is 0 — so 10 is even.

step 4: Try another example

Now let's try n = 7. In binary: 0111

7 & 1 = 0111 & 0001 = 0001 → which is 1

So 7 is odd.

Edge Cases

  • n = 0: Binary is 0000. 0 & 1 = 0 → even. Works fine.
  • n = -3: Binary representation in 2's complement still preserves the LSB. -3 & 1 = 1 → odd. Also works correctly.
  • Very large numbers: The bitwise operation is constant time (O(1)), so it's extremely fast regardless of how large the number is.

Final Thoughts

Using n & 1 to determine whether a number is even or odd is a smart and efficient trick rooted in binary logic. It's especially helpful in performance-critical applications like competitive programming or embedded systems where every CPU cycle counts.

Remember, this method works reliably for both positive and negative integers because the least significant bit always reflects parity, even in 2’s complement representation.

Algorithm Steps

  1. Use bitwise AND with 1: n & 1.
  2. If the result is 0, then the number is even (LSB is 0).
  3. If the result is 1, then the number is odd (LSB is 1).

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#include <stdio.h>

int main() {
    int n = 42; // Change this number to test
    if ((n & 1) == 0) {
        printf("%d is even\n", n);
    } else {
        printf("%d is odd\n", n);
    }
    return 0;
}

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(1)The operation uses a single bitwise AND operation (`n & 1`), which is executed in constant time regardless of the input value.
Average CaseO(1)Bitwise operations are constant-time operations. No matter the size of the number, checking the least significant bit takes the same amount of time.
Worst CaseO(1)Even in the worst case, only one bitwise AND operation and one conditional check are performed. These are constant-time operations.

Space Complexity

O(1)

Explanation: The algorithm does not use any extra space apart from a few primitive variables. It operates entirely in-place using bitwise checks.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...