Using Bitwise Operators
Bitwise operators work directly with the bits of a number, making them a powerful tool in programming, especially when you need to perform checks or manipulations at the binary level. For checking if a number is odd or even, we can use the bitwise AND operator (&).
Here’s the reasoning:
In binary, even numbers always end in 0 (like 10 in binary is 1010).
Odd numbers end in 1 (like 3 in binary is 11).
Using the bitwise AND operator with 1 (number & 1) lets us directly check the last bit of a number:
-
If number & 1 equals 0, the number is even.
-
If number & 1 equals 1, the number is odd.
Let’s see how we can implement this in a Java program:
Java
public class BitwiseOddEvenChecker {
public static void main(String[] args) {
// Example number
int number = 14; // You can change this to test other numbers
// Using bitwise AND operator to check if the number is odd or even
if ((number & 1) == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
14 is even
In this example:
-
We define a class named BitwiseOddEvenChecker.
-
We declare an integer number and assign it a value in the main method.
-
We apply the bitwise AND operation between number and 1.
-
Based on the result, we print whether the number is even or odd.
Note : This method is efficient and very useful where performance matters, as bitwise operations are generally faster than arithmetic operations.
Using Bitwise OR
The bitwise OR operator (|) can also be utilized in programming to check whether a number is odd or even, although it's less direct than the bitwise AND method. Typically, the bitwise OR isn't used alone to determine the parity of a number, but it can help illustrate how bits operate in conditions related to evenness or oddness.
Here's a general explanation:
-
Bitwise OR compares each bit of two numbers. If at least one of the bits is 1, the result is 1; otherwise, it’s 0.
-
For our purposes, we don’t typically use | to check parity directly, but we can modify our approach to see its effect in combination with other operations.
Let's see a java program that uses this method :
Java
public class ConceptualORChecker {
public static void main(String[] args) {
// Example number
int number = 15; // You can change this to test other numbers
// Conceptually using bitwise OR to check if the number is odd or even
int result = number | 1;
// The result will always be odd if the original number is odd
if (result == number) {
System.out.println(number + " is odd.");
} else {
System.out.println(number + " is even."); // This line is for demonstration and typically won't execute in this setup.
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
15 is odd.
In this code:
-
We define a class called ConceptualORChecker.
-
Inside the main method, we declare an integer number and assign a value.
-
We perform a bitwise OR operation between number and 1.
- We compare the result with the original number. If they are the same, it implies the last bit was already 1 (odd number).
Using Bitwise AND
The bitwise AND operator (&) is another useful tool in checking whether a number is odd or even, and it is one of the most straightforward methods when working directly with binary values of numbers. This method focuses on the least significant bit (LSB) of the number, which determines its oddness or evenness.
Here’s how it works:
-
In binary representation, the least significant bit (LSB) is the rightmost bit. For even numbers, this bit is 0, and for odd numbers, it is 1.
-
When you perform a bitwise AND between a number and 1 (number & 1), it checks just the LSB.
-
If the LSB is 0 (as in even numbers), the result of the operation will be 0.
-
If the LSB is 1 (as in odd numbers), the result will be 1.
Let’s see a Java example of this method:
Java
public class BitwiseANDEvenOddChecker {
public static void main(String[] args) {
// Example number
int number = 22; // You can change this to test other numbers
// Using bitwise AND to determine if the number is even or odd
if ((number & 1) == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
22 is even
In this code:
-
The class BitwiseANDEvenOddChecker is defined.
-
Within the main method, an integer number is declared and assigned a value.
-
The bitwise AND operation (&) is applied between number and 1.
-
Depending on whether the result is 0 or 1, it prints out if the number is even or odd.
Note : This method is highly effective and efficient, especially when you need to perform this check multiple times, as it reduces the computational load by avoiding division and directly checks the bit value.
Using Bitwise XOR
The bitwise XOR operator (^) is a tool that can be utilized to toggle certain bits in a number. However, it's not typically used for checking if a number is odd or even, but it could be useful to learn to understand basic working of XOR.
Here's how the XOR operation works:
-
The XOR operation takes two bits and returns 1 if the bits are different, and 0 if they are the same.
-
Applying XOR between a number and itself always results in zero because all bits cancel each other out.
While XOR isn’t directly used to determine the oddness or evenness of a number, let's see a Java example using this method :
Java
public class BitwiseXORChecker {
public static void main(String[] args) {
// Example number
int number = 5; // You can change this to test other numbers
// Hypothetical use of XOR to check if the number is odd or even
int result = number ^ 1; // Toggles the LSB of the number
// Compare the result with the original number
if (result == number + 1) {
System.out.println(number + " is odd.");
} else {
System.out.println(number + " is even.");
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
5 is odd
In this code:
-
We define a class BitwiseXORChecker.
-
Within the main method, we assign a value to the integer number.
-
We perform a bitwise XOR operation between number and 1, which toggles the least significant bit.
-
If toggling the LSB turns the number into the next higher number, it indicates it was odd (since only odd numbers become even when their LSB is toggled from 1 to 0).
- Conversely, if it doesn't match the pattern, the number is even.
By Checking the Least Significant Bit
Checking the Least Significant Bit (LSB) is perhaps the most direct and efficient way to determine if a number is odd or even. The LSB is the rightmost bit in a number's binary representation, and it directly indicates the number's parity:
-
If the LSB is 0, the number is even because it can be divided completely by 2.
-
If the LSB is 1, the number is odd because dividing by 2 leaves a remainder of 1.
Let’s see a Java example using this method :
Java
public class LSBChecker {
public static void main(String[] args) {
// Example number
int number = 29; // You can change this to test other numbers
// Checking the least significant bit to determine parity
if ((number & 1) == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
29 is odd
In this code:
-
We define a class called LSBChecker.
-
In the main method, an integer number is assigned a value.
-
The expression (number & 1) checks the LSB of the number. This is done by using the bitwise AND operation with 1.
-
If the result is 0, it prints that the number is even; if the result is 1, it prints that the number is odd.
Note : This method is not only simple but also very fast, making it ideal for use in programs that require frequent parity checks without the overhead of more complex computations.
Frequently Asked Questions
Why do we use the modulo operation to check if a number is odd or even?
The modulo operation (%) returns the remainder after dividing one number by another. When you check a number with % 2, if the remainder is 0, the number is even; if it's 1, the number is odd. This is a straightforward way to determine the number's parity.
Are bitwise operations faster than using the modulo operation for checking number parity?
Yes, bitwise operations are generally faster than using the modulo operation because they deal directly with the number's bits and require fewer computational steps.
Can these methods be used with any programming language?
While the examples provided are in Java, the concepts of using modulo and bitwise operations apply across most programming languages that support these operations, such as C++, Python, and JavaScript.
Conclusion
In this article, we have discussed various methods to determine if a number is odd or even in Java. We started with the simple modulo method, then we saw more efficient techniques using bitwise operations, such as AND and XOR. These methods offer a better understanding of how numbers are represented and manipulated at the binary level, which provides efficient solution.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.