Bit wise odd or even


Odd or Even – GFG


Problem Statement

Given an integer n, determine whether it is even or odd.

Return:


Examples

Example 1:

Input: n = 1
Output: false
Explanation: 1 is odd.

Example 2:

Input: n = 2
Output: true
Explanation: 2 is even.

Constraints


Intuition

In binary representation:

So, we can check the parity of a number by checking if the LSB is set using a bitwise AND with 1.


Approach: Bitwise AND with 1

To check whether a number is even:

This is because:


Java Code

class Solution {
    static boolean isEven(int n) {
       return (n & 1) == 0; 
    }
}

Time and Space Complexity

Metric Value
Time Complexity O(1)
Space Complexity O(1)
Explanation Uses a single bitwise operation, no loops or extra space

Dry Run

Input: n = 2

Input: n = 3


Conclusion

This problem is a basic yet powerful example of how bitwise operations can optimize simple checks like odd/even determination.

Using (n & 1) is faster than using % 2 and is widely used in low-level or performance-critical code.