Check Even/Odd Using Bitwise AND

Check Even/Odd Using Bitwise AND

Medium C++ Operators 28 views
Explanation Complexity

Problem Statement

Use bitwise AND with 1 to check even/odd instantly.
Real Life: Fastest way to check even/odd in programming.

Input Format

An integer N.

Output Format

Print

• Even
OR

• Odd

Example

7
Odd

Constraints

• Integer value

• Works for positive and negative numbers

Concept Explanation

The least significant bit (LSB) decides even or odd:

• If LSB is 0 → Even

• If LSB is 1 → Odd
Bitwise AND with 1 checks this instantly.

Step-by-Step Explanation

1.Take input number N.

2.Apply bitwise AND: N & 1.

3.If result is 0:

• Print Even.

4.Else:

• Print Odd.

Concept Explanation

The least significant bit (LSB) decides even or odd:

• If LSB is 0 → Even

• If LSB is 1 → Odd
Bitwise AND with 1 checks this instantly.

Step-by-Step Explanation

1.Take input number N.

2.Apply bitwise AND: N & 1.

3.If result is 0:

• Print Even.

4.Else:

• Print Odd.

Input / Output Format

Input Format
An integer N.
Output Format
Print

• Even
OR

• Odd
Constraints
• Integer value

• Works for positive and negative numbers

Examples

Input:
7
Output:
Odd

Example Solution (Public)

C++
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;
        }
    }
}

Official Solution Code

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;
        }
    }
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.