Monday, November 18, 2024
Google search engine
HomeData Modelling & AIBitwise AND of all even number up to N

Bitwise AND of all even number up to N

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)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments