WikiGalaxy

Personalize

Checking Even or Odd Using Bits

Understanding Bitwise Operations

Bitwise operations allow us to manipulate individual bits of a number. To determine if a number is even or odd, we can use the bitwise AND operation with the number 1.

Concept Explanation

In binary representation, even numbers have their least significant bit (LSB) as 0, while odd numbers have it as 1. By performing an AND operation with 1, we can check the LSB directly.


int number = 5;
if ((number & 1) == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}
    

Example 1: Single Number Check

This example demonstrates checking whether a single number is even or odd using bitwise operations.

Console Output:

Odd

Batch Processing of Numbers

Processing Multiple Numbers

We can extend the bitwise technique to process an array of numbers, checking each for even or odd status efficiently.


int[] numbers = {10, 23, 36, 47, 58};
for (int number : numbers) {
    if ((number & 1) == 0) {
        System.out.println(number + " is Even");
    } else {
        System.out.println(number + " is Odd");
    }
}
    

Example 2: Array Check

This example illustrates processing an array of integers to determine their parity using bitwise operations.

Console Output:

10 is Even

23 is Odd

36 is Even

47 is Odd

58 is Even

Using Functions for Modularity

Encapsulating Logic in Functions

To improve code reusability and readability, encapsulate the even/odd check logic within a function.


public static boolean isEven(int number) {
    return (number & 1) == 0;
}

public static void main(String[] args) {
    int number = 42;
    if (isEven(number)) {
        System.out.println(number + " is Even");
    } else {
        System.out.println(number + " is Odd");
    }
}
    

Example 3: Function-Based Check

This example shows how to use a function to determine if a number is even or odd, enhancing modularity.

Console Output:

42 is Even

Using Streams for Processing

Leveraging Java Streams

Java Streams provide a powerful way to process collections of objects. Use streams to filter even and odd numbers.


import java.util.Arrays;

public static void main(String[] args) {
    int[] numbers = {3, 6, 9, 12, 15};
    Arrays.stream(numbers)
          .forEach(number -> {
              if ((number & 1) == 0) {
                  System.out.println(number + " is Even");
              } else {
                  System.out.println(number + " is Odd");
              }
          });
}
    

Example 4: Stream Processing

This example demonstrates the use of Java Streams to process an array of numbers for even and odd classification.

Console Output:

3 is Odd

6 is Even

9 is Odd

12 is Even

15 is Odd

Handling Large Numbers

Bitwise Operations on Large Numbers

Bitwise operations are efficient and can handle large numbers effectively, making them suitable for high-performance applications.


public static void main(String[] args) {
    long largeNumber = 123456789012345L;
    if ((largeNumber & 1) == 0) {
        System.out.println(largeNumber + " is Even");
    } else {
        System.out.println(largeNumber + " is Odd");
    }
}
    

Example 5: Large Number Check

This example highlights the use of bitwise operations to check the parity of a large number.

Console Output:

123456789012345 is Odd

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025