Check if a Number is Odd or Even using Bitwise Operators - GeeksforGeeks

11 Nov, 2021
Given a number N, the task is to check whether the number is even or odd using Bitwise Operators.Examples:  
Input: N = 11 Output: OddInput: N = 10 Output: Even  
 
Following Bitwise Operators can be used to check if a number is odd or even: 
1. Using Bitwise XOR operator: The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even. As we know bitwise XOR Operation of the Number by 1 increment the value of the number by 1 if the number is even otherwise it decrements the value of the number by 1 if the value is odd.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to check for even or odd// using Bitwise XOR operator #include <iostream>using namespace std; // Returns true if n is even, else oddbool isEven(int n){     // n^1 is n+1, then even, else odd    if (n ^ 1 == n + 1)        return true;    else        return false;} // Driver codeint main(){    int n = 100;    isEven(n)? cout << "Even": cout << "Odd";     return 0;}
// Java program to check for even or odd// using Bitwise XOR operatorclass GFG{     // Returns true if n is even, else odd    static boolean isEven(int n)    {         // n^1 is n+1, then even, else odd        if ((n ^ 1) == n + 1)            return true;        else            return false;    }     // Driver code    public static void main(String[] args)    {        int n = 100;        System.out.print(isEven(n) == true ? "Even" : "Odd");    }} // This code is contributed by Rajput-Ji
# Python3 program to check for even or odd# using Bitwise XOR operator # Returns true if n is even, else odddef isEven( n) :     # n^1 is n+1, then even, else odd    if (n ^ 1 == n + 1) :        return True;    else :        return False; # Driver codeif __name__ == "__main__" :    n = 100;    print("Even") if isEven(n) else print( "Odd"); # This code is contributed by AnkitRai01
// C# program to check for even or odd// using Bitwise XOR operatorusing System; class GFG{     // Returns true if n is even, else odd    static bool isEven(int n)    {         // n^1 is n+1, then even, else odd        if ((n ^ 1) == n + 1)            return true;        else            return false;    }     // Driver code    public static void Main(String[] args)    {        int n = 100;        Console.Write(isEven(n) == true ? "Even" : "Odd");    }} // This code is contributed by Rajput-Ji
<script> // JavaScript program to check for even or odd// using Bitwise XOR operator // Returns true if n is even, else oddfunction isEven(n){     // n^1 is n+1, then even, else odd    if (n ^ 1 == n + 1)        return true;    else        return false;} // Driver code     let n = 100;    isEven(n)? document.write("Even"): document.write("Odd"); // This code is contributed by Surbhi Tyagi. </script>
Even
2. Using Bitwise AND operator: The idea is to check whether the last bit of the number is set or not. If last bit is set then the number is odd, otherwise even.As we know bitwise AND Operation of the Number by 1 will be 1, If it is odd because the last bit will be already set. Otherwise it will give 0 as output. 
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to check for even or odd// using Bitwise AND operator#include <iostream>using namespace std;   // Returns true if n is even, else oddbool isEven(int n){    // n&1 is 1, then odd, else even    return (!(n & 1));}   // Driver codeint main(){    int n = 101;    isEven(n)    ? cout << "Even"    : cout << "Odd";    return 0;}
// Java program to check for even or odd// using Bitwise AND operatorclass GFG{     // Returns true if n is even, else oddstatic boolean isEven(int n){    // n&1 is 1, then odd, else even    return ((n & 1)!=1);}     // Driver codepublic static void main(String[] args){    int n = 101;    System.out.print(isEven(n) == true ? "Even" : "Odd");}} // This code is contributed by PrinciRaj1992
# Python3 program to check for even or odd# using Bitwise AND operator # Returns true if n is even, else odddef isEven(n) :     # n&1 is 1, then odd, else even    return (not(n & 1)); # Driver codeif __name__ == "__main__" :     n = 101;    if isEven(n) :        print("Even")    else:        print("Odd"); # This code is contributed by AnkitRai01
// C# program to check for even or odd// using Bitwise AND operatorusing System; class GFG{         // Returns true if n is even, else odd    static bool isEven(int n)    {        // n&1 is 1, then odd, else even        return ((n & 1) != 1);    }             // Driver code    public static void Main()    {        int n = 101;        Console.Write(isEven(n) == true ? "Even" : "Odd");    }} // This code is contributed by AnkitRai01
<script>// javascript program to check for even or odd// using Bitwise AND operator       // Returns true if n is even, else oddfunction isEven(n){    // n&1 is 1, then odd, else even    return ((n & 1)!=1);}     // Driver codevar n = 101;document.write(isEven(n) == true ? "Even" : "Odd"); // This code is contributed by Princi Singh</script>
Odd
3. Using Bitwise OR operator: The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even. As we know bitwise OR Operation of the Number by 1 increment the value of the number by 1 if the number is even otherwise it will remain unchanged. So, if after OR operation of number with 1 gives a result which is greater than the number then it is even and we will return true otherwise it is odd and we will return false.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to check for even or odd// using Bitwise OR operator #include <iostream>using namespace std; // Returns true if n is even, else oddbool isEven(int n){     // n|1 is greater than n, then even, else odd    if ((n | 1) > n)        return true;    else        return false;} // Driver codeint main(){    int n = 100;    isEven(n)    ? cout << "Even"    : cout << "Odd";     return 0;}
// Java program to check for even or odd// using Bitwise OR operatorclass GFG{    // Returns true if n is even, else odd    static boolean isEven(int n)    {         // n|1 is greater than n, then even, else odd        if ((n | 1) > n)            return true;        else            return false;    }     // Driver code    public static void main(String[] args)    {        int n = 100;        System.out.print(isEven(n) == true ? "Even" : "Odd");    }}
# Python3 program to check for even or odd# using Bitwise OR operator # Returns true if n is even, else odddef isEven( n) :     # n|1 is greater then n, then even, else odd    if (n | 1 > n) :        return True;    else :        return False; # Driver codeif __name__ == "__main__" :    n = 100;    print("Even") if isEven(n) else print( "Odd");
// C# program to check for even or odd// using Bitwise XOR operatorusing System; class GFG{     // Returns true if n is even, else odd    static bool isEven(int n)    {         // n|1 is greater then n, then even, else odd        if ((n | 1) > n)            return true;        else            return false;    }     // Driver code    public static void Main(String[] args)    {        int n = 100;        Console.Write(isEven(n) == true ? "Even" : "Odd");    }} // This code is contributed by Rajput-Ji
<script>// javascript program to check for even or odd// using Bitwise OR operator // Returns true if n is even, else oddfunction isEven(n){     // n|1 is greater than n, then even, else odd    if ((n | 1) > n)        return true;    else        return false;} // Driver codevar n = 100;document.write(isEven(n) == true ? "Even" : "Odd"); // This code is contributed by Amit Katiyar.</script>
Even
Time Complexity: O(1)
Rajput-Ji
ankthon
princiraj1992
coder_nero
surbhityagi15
princi singh
amit143katiyar
ahampriyanshu
Bitwise-AND
Bitwise-XOR
Bit Magic
School Programming
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cyclic Redundancy Check and Modulo-2 Division
Add two numbers without using arithmetic operators
Josephus problem | Set 1 (A O(n) Solution)
Set, Clear and Toggle a given bit of a number in C
Bit Fields in C
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects