Bitwise AND of all even number up to N

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

Examples:

Input: 2
Output: 2

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

Naive approach: Initialize result as 2. Iterate loop from 4 to n (for all even number) and update result by finding bitwise and (&).



Below is the implementation of the approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

2

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ imlpementation 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java imlpementation 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 imlpementation 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# imlpementation 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

chevron_right


Output:

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ANKITKUMAR34, AnkitRai01

Article Tags :
Practice Tags :


5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.