Given a number N, the task is to check whether the number is even or odd using Bitwise Operators.
Examples:
Input: N = 11
Output: Odd
Input: 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++
#include <iostream>
using namespace std;
bool isEven( int n)
{
if ((n ^ 1) == (n + 1))
return true ;
else
return false ;
}
int main()
{
int n = 100;
isEven(n) ? cout << "Even" : cout << "Odd" ;
return 0;
}
|
Java
class GFG {
static boolean isEven( int n)
{
if ((n ^ 1 ) == (n + 1 ))
return true ;
else
return false ;
}
public static void main(String[] args)
{
int n = 100 ;
System.out.print(isEven(n) == true ? "Even"
: "Odd" );
}
}
|
Python3
def isEven(n):
if ((n ^ 1 ) = = (n + 1 )):
return True
else :
return False
if __name__ = = "__main__" :
n = 100
print ( "Even" ) if isEven(n) else print ( "Odd" )
|
C#
using System;
class GFG {
static bool isEven( int n)
{
if ((n ^ 1) == (n + 1))
return true ;
else
return false ;
}
public static void Main(String[] args)
{
int n = 100;
Console.Write(isEven(n) == true ? "Even" : "Odd" );
}
}
|
Javascript
<script>
function isEven(n)
{
if ((n ^ 1) == (n + 1))
return true ;
else
return false ;
}
let n = 100;
isEven(n)
? document.write( "Even" )
: document.write( "Odd" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
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++
#include <iostream>
using namespace std;
bool isEven( int n)
{
return (!(n & 1));
}
int main()
{
int n = 101;
isEven(n) ? cout << "Even" : cout << "Odd" ;
return 0;
}
|
Java
class GFG {
static boolean isEven( int n)
{
return ((n & 1 ) != 1 );
}
public static void main(String[] args)
{
int n = 101 ;
System.out.print(isEven(n) == true ? "Even"
: "Odd" );
}
}
|
Python3
def isEven(n):
return ( not (n & 1 ))
if __name__ = = "__main__" :
n = 101
if isEven(n):
print ( "Even" )
else :
print ( "Odd" )
|
C#
using System;
class GFG {
static bool isEven( int n)
{
return ((n & 1) != 1);
}
public static void Main()
{
int n = 101;
Console.Write(isEven(n) == true ? "Even" : "Odd" );
}
}
|
Javascript
<script>
function isEven(n)
{
return ((n & 1)!=1);
}
var n = 101;
document.write(isEven(n) == true ? "Even" : "Odd" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
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 that 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++
#include <iostream>
using namespace std;
bool isEven( int n)
{
if ((n | 1) > n)
return true ;
else
return false ;
}
int main()
{
int n = 100;
isEven(n) ? cout << "Even" : cout << "Odd" ;
return 0;
}
|
Java
class GFG {
static boolean isEven( int n)
{
if ((n | 1 ) > n)
return true ;
else
return false ;
}
public static void main(String[] args)
{
int n = 100 ;
System.out.print(isEven(n) == true ? "Even"
: "Odd" );
}
}
|
Python3
def isEven(n):
if (n | 1 > n):
return True
else :
return False
if __name__ = = "__main__" :
n = 100
print ( "Even" ) if isEven(n) else print ( "Odd" )
|
C#
using System;
class GFG {
static bool isEven( int n)
{
if ((n | 1) > n)
return true ;
else
return false ;
}
public static void Main(String[] args)
{
int n = 100;
Console.Write(isEven(n) == true ? "Even" : "Odd" );
}
}
|
Javascript
<script>
function isEven(n)
{
if ((n | 1) > n)
return true ;
else
return false ;
}
var n = 100;
document.write(isEven(n) == true ? "Even" : "Odd" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
4. Using bitwise left and right shift operators: The idea is to check whether a number remains the same after performing some operations i.e. bitwise left and right shift. When we do a bitwise right shift of the number then the last bit of the number is removed whenever it is 1 or 0. After that performing the left shift to the number by default a new binary is added to the last bit which is 0, by displacement of one bit by one. Even numbers remain the same but odd changed its value. compare the initial and final values of the number to check number is even or odd.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int main()
{
int num = 2;
if (num == (num >> 1) << 1) {
cout << num << " is even." << endl;
}
else {
cout << num << " is not even." << endl;
}
return 0;
}
|
C
#include <stdio.h>
int main() {
int num = 2 ;
if (num == ( num>>1 ) << 1){
printf ( "%d is even.\n" ,num);
}
else {
printf ( "%d is not even.\n" ,num);
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void main(String[] args)
{
int num = 2 ;
if (num == ( num>> 1 ) << 1 ){
System.out.println(num + " is even.\n" );
}
else {
System.out.println(num + " is not even.\n" );
}
}
}
|
Python3
num = 2 ;
if (num = = ( num>> 1 ) << 1 ):
print (num, "is even.\n" );
else :
print (num, "is not even.\n" );
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
int num = 2 ;
if (num == ( num>>1 ) << 1){
Console.Write(num+ " is even.\n" );
}
else {
Console.Write(num+ " is not even.\n" );
}
}
}
|
Javascript
let num = 2 ;
if (num == ( num>>1 ) << 1){
console.log(num, "is even.\n" );
}
else {
console.log(num, "is not even.\n" );
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)