Bitwise OR( | ) of all even number from 1 to N

Given a number N, the task is to find the bitwise OR( | ) of all even number from 1 to N.

Examples:

Input: 2
Output: 2

Input: 10
Output: 14
Explanation: 2 | 4 | 6 | 8 | 10 = 14

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



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 OR
// of all the even numbers upto N
int bitwiseOrTillN(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 = 10;
    cout << bitwiseOrTillN(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 OR 
    // of all the even numbers upto N 
    static int bitwiseOrTillN(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 
    static public void main (String args[])
    
        int n = 10
        System.out.println(bitwiseOrTillN(n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the above approach
  
# Function to return the bitwise OR
# of all the even numbers upto N
def bitwiseOrTillN ( n ):
      
    # Initialize result as 2
    result = 2;
  
    for i in range(4, n + 1, 2) :
        result = result | i
      
    return result
  
# Driver code
n = 10;
print(bitwiseOrTillN(n));
  
# This code is contributed by ANKITKUMAR34

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 OR 
    // of all the even numbers upto N 
    static int bitwiseOrTillN(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 
    static public void Main ()
    
        int n = 10; 
        Console.WriteLine(bitwiseOrTillN(n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

14

Efficient Approach: Compute the total number of bits in N. In bitwise OR, the rightmost bit will be 0 and all other bits will be 1. Therefore, return pow(2, total no. of bits)-2. It will give the equivalent value in decimal of bitwise OR.

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>
#include <math.h>
using namespace std;
  
// Function to return the bitwise OR
// of all even numbers upto N
int bitwiseOrTillN(int n)
{
    // For value less than 2
    if (n < 2)
        return 0;
  
    // Count total number of bits in bitwise or
    // all bits will be set except last bit
    int bitCount = log2(n) + 1;
  
    // Compute 2 to the power bitCount and subtract 2
    return pow(2, bitCount) - 2;
}
  
// Driver code
int main()
{
    int n = 10;
    cout << bitwiseOrTillN(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 OR 
    // of all even numbers upto N 
    static int bitwiseOrTillN(int n) 
    
        // For value less than 2 
        if (n < 2
            return 0
      
        // Count total number of bits in bitwise or 
        // all bits will be set except last bit 
        int bitCount = (int)(Math.log(n)/Math.log(2)) + 1
      
        // Compute 2 to the power bitCount and subtract 2 
        return (int)Math.pow(2, bitCount) - 2
    
      
    // Driver code 
    public static void main (String[] args)
    
        int n = 10
        System.out.println(bitwiseOrTillN(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 
from math import log2
  
# Function to return the bitwise OR 
# of all even numbers upto N 
def bitwiseOrTillN(n) : 
  
    # For value less than 2 
    if (n < 2) :
        return 0
  
    # Count total number of bits in bitwise or 
    # all bits will be set except last bit 
    bitCount = int(log2(n)) + 1
  
    # Compute 2 to the power bitCount and subtract 2 
    return pow(2, bitCount) - 2
  
# Driver code 
if __name__ == "__main__"
  
    n = 10
    print(bitwiseOrTillN(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 OR 
    // of all even numbers upto N 
    static int bitwiseOrTillN(int n) 
    
        // For value less than 2 
        if (n < 2) 
            return 0; 
      
        // Count total number of bits in bitwise or 
        // all bits will be set except last bit 
        int bitCount = (int)(Math.Log(n)/Math.Log(2)) + 1; 
      
        // Compute 2 to the power bitCount and subtract 2 
        return (int)Math.Pow(2, bitCount) - 2; 
    
      
    // Driver code 
    public static void Main()
    
        int n = 10; 
        Console.WriteLine(bitwiseOrTillN(n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

14

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