Open In App

Bitwise AND of all even number up to N

Last Updated : 28 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to find bitwise and (&) of all even numbers from 1 to N.

Examples: 

Input:
Output: 2

Input :10 
Output :
Explanation: Bitwise and of 2, 4, 6, 8 and 10 are 0. 

Naive approach: Initialize result as 2. Iterate the loop from 4 to n (for all even numbers) and update the result by finding bitwise and (&).
Below is the implementation of the approach:

C++




// C++ implementation of the above approach
#include <iostream>
using namespace std;
 
// Function to return the bitwise &
// of all the even numbers upto N
int bitwiseAndTillN(int n)
{
    // Initialize result as 2
    int result = 2;
 
    for (int i = 4; i <= n; i = i + 2) {
        result = result & i;
    }
    return result;
}
 
// Driver code
int main()
{
    int n = 2;
    cout << bitwiseAndTillN(n);
    return 0;
}


Java




// Java implementation of the above approach
class GFG
{
     
    // Function to return the bitwise &
    // of all the even numbers upto N
    static int bitwiseAndTillN(int n)
    {
        // Initialize result as 2
        int result = 2;
     
        for (int i = 4; i <= n; i = i + 2)
        {
            result = result & i;
        }
        return result;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 2;
        System.out.println(bitwiseAndTillN(n));
    }
}
 
// This code is contributed by AnkitRai01


Python3




# Python3 implementation of the above approach
 
# Function to return the bitwise &
# of all the even numbers upto N
def bitwiseAndTillN(n) :
 
    # Initialize result as 2
    result = 2;
 
    for i in range(4, n + 1, 2) :
        result = result & i;
     
    return result;
 
# Driver code
if __name__ == "__main__" :
     
    n = 2;
    print(bitwiseAndTillN(n));
 
# This code is contributed by AnkitRai01


C#




// C# implementation of the above approach
using System;
 
class GFG
{
     
    // Function to return the bitwise &
    // of all the even numbers upto N
    static int bitwiseAndTillN(int n)
    {
        // Initialize result as 2
        int result = 2;
     
        for (int i = 4; i <= n; i = i + 2)
        {
            result = result & i;
        }
        return result;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 2;
        Console.WriteLine(bitwiseAndTillN(n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
// Javascript implementation of the above approach
 
// Function to return the bitwise &
// of all the even numbers upto N
function bitwiseAndTillN(n) {
    // Initialize result as 2
    let result = 2;
 
    for (let i = 4; i <= n; i = i + 2) {
        result = result & i;
    }
    return result;
}
 
// Driver code
 
let n = 2;
document.write(bitwiseAndTillN(n));
</script>


Output: 

2

 

Time Complexity: O(n), where n is the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Efficient approach: Efficient approach is to return 2 for N less than 4 and return 0 for all N>=4 because bitwise and of 2 and 4 is 0 and bitwise and of 0 with any number is 0.

Below is the implementation of the approach: 

C++




// C++ implementation of the above approach
#include <iostream>
using namespace std;
 
// Function to return the bitwise &
// of all the numbers upto N
int bitwiseAndTillN(int n)
{
    if (n < 4)
        return 2;
    else
        return 0;
}
 
int main()
{
    int n = 2;
    cout << bitwiseAndTillN(n);
    return 0;
}


Java




// Java implementation of the above approach
class GFG
{
     
    // Function to return the bitwise &
    // of all the numbers upto N
    static int bitwiseAndTillN(int n)
    {
        if (n < 4)
            return 2;
        else
            return 0;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 2;
        System.out.println(bitwiseAndTillN(n));
    }
}
 
// This code is contributed by AnkitRai01


Python3




# Python3 implementation of the above approach
 
# Function to return the bitwise &
# of all the numbers upto N
def bitwiseAndTillN( n):
    if (n < 4):
        return 2
    else:
        return 0
 
# Driver code
n = 2
print(bitwiseAndTillN(n))
 
# This code is contributed by ANKITKUMAR34


C#




// C# implementation of the above approach
using System;
 
class GFG
{
     
    // Function to return the bitwise &
    // of all the numbers upto N
    static int bitwiseAndTillN(int n)
    {
        if (n < 4)
            return 2;
        else
            return 0;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 2;
        Console.WriteLine(bitwiseAndTillN(n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
 
// JavaScript implementation of the above approach
 
// Function to return the bitwise &
// of all the numbers upto N
function bitwiseAndTillN(n)
{
    if (n < 4)
        return 2;
    else
        return 0;
}
 
// driver code
 
    let n = 2;
    document.write (bitwiseAndTillN(n));
     
 // this code is contributed by shivanisinghss2110 
  
</script>


Output: 

2

 

Time complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads