Check if a Number is Even/Odd

Problem and examples

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.

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

Explore Check if a Number is Even/Odd

Load an example or enter your own input. Use the player controls to follow each change.

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.

  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).

Implementation and efficiency

C implementation

#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;
}

C++ implementation

#include <iostream>
using namespace std;

int main() {
    int n = 42; // Change this number to test
    if ((n & 1) == 0) {
        cout << n << " is even" << endl;
    } else {
        cout << n << " is odd" << endl;
    }
    return 0;
}

Python implementation

n = 42  # Change this number to test
if (n & 1) == 0:
    print(f"{n} is even")
else:
    print(f"{n} is odd")

Java implementation

public class EvenOddChecker {
    public static void main(String[] args) {
        int n = 42; // Change this number to test
        if ((n & 1) == 0) {
            System.out.println(n + " is even");
        } else {
            System.out.println(n + " is odd");
        }
    }
}

JavaScript implementation

const n = 42; // Change this number to test
if ((n & 1) === 0) {
  console.log(`${n} is even`);
} else {
  console.log(`${n} is odd`);
}

Go implementation

package main

import "fmt"

func main() {
    n := 42 // Change this number to test
    if n&1 == 0 {
        fmt.Printf("%d is even\n", n)
    } else {
        fmt.Printf("%d is odd\n", n)
    }
}

Rust implementation

fn main() {
    let n = 42; // Change this number to test
    if n & 1 == 0 {
        println!("{} is even", n);
    } else {
        println!("{} is odd", n);
    }
}

Kotlin implementation

fun main() {
    val n = 42 // Change this number to test
    if (n and 1 == 0) {
        println("$n is even")
    } else {
        println("$n is odd")
    }
}

Swift implementation

let n = 42 // Change this number to test
if n & 1 == 0 {
    print("\(n) is even")
} else {
    print("\(n) is odd")
}

TypeScript implementation

const n: number = 42; // Change this number to test
if ((n & 1) === 0) {
  console.log(`${n} is even`);
} else {
  console.log(`${n} is odd`);
}

Time complexity

CaseTimeExplanation
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)

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