odd even using bitwise operator

In this article we are going to see the code of odd even in java using bitwise operator

odd even using bitwise OR

package gangforcode;

import java.util.Scanner;

public class oddEven {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // take input from usera
        int n = sc.nextInt();
        if((n | 1) >n)
            System.out.println("Even");
        else System.out.println("Odd");
    }
}

Output

odd even using bitwise or

Odd even using bitwise AND

package gangforcode;

import java.util.Scanner;

public class oddEven {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // take input from usera
        int n = sc.nextInt();
        if((n & 1) != 1)
            System.out.println("Even");
        else System.out.println("Odd");
    }
}

Output

odd even using bitwise and

Thanks !!

Leave a Comment