Bitwise OR( | ) of all even number from 1 to N
Last Updated :
28 Dec, 2022
Given a number N, the task is to find the bitwise OR( | ) of all even numbers from 1 to N.
Examples:
Input: 2
Output: 2
Input: 10
Output: 14
Explanation: 2 | 4 | 6 | 8 | 10 = 14
Naive Approach:
- Initialize the result as 2.
- Iterate the loop from 4 to n (for all even number) and update result by finding bitwise or ( | ).
Below is the implementation of the approach:
C++
#include <iostream>
using namespace std;
int bitwiseOrTillN( int n)
{
int result = 2;
for ( int i = 4; i <= n; i = i + 2) {
result = result | i;
}
return result;
}
int main()
{
int n = 10;
cout << bitwiseOrTillN(n);
return 0;
}
|
Java
class GFG
{
static int bitwiseOrTillN( int n)
{
int result = 2 ;
for ( int i = 4 ; i <= n; i = i + 2 )
{
result = result | i;
}
return result;
}
static public void main (String args[])
{
int n = 10 ;
System.out.println(bitwiseOrTillN(n));
}
}
|
Python3
def bitwiseOrTillN ( n ):
result = 2 ;
for i in range ( 4 , n + 1 , 2 ) :
result = result | i
return result
n = 10 ;
print (bitwiseOrTillN(n));
|
C#
using System;
class GFG
{
static int bitwiseOrTillN( int n)
{
int result = 2;
for ( int i = 4; i <= n; i = i + 2)
{
result = result | i;
}
return result;
}
static public void Main ()
{
int n = 10;
Console.WriteLine(bitwiseOrTillN(n));
}
}
|
Javascript
<script>
function bitwiseOrTillN(n)
{
var result = 2;
for ( var i = 4; i <= n; i = i + 2)
{
result = result | i;
}
return result;
}
var n = 10;
document.write( bitwiseOrTillN(n));
</script>
|
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: 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++
#include <iostream>
#include <math.h>
using namespace std;
int bitwiseOrTillN( int n)
{
if (n < 2)
return 0;
int bitCount = log2(n) + 1;
return pow (2, bitCount) - 2;
}
int main()
{
int n = 10;
cout << bitwiseOrTillN(n);
return 0;
}
|
Java
class GFG
{
static int bitwiseOrTillN( int n)
{
if (n < 2 )
return 0 ;
int bitCount = ( int )(Math.log(n)/Math.log( 2 )) + 1 ;
return ( int )Math.pow( 2 , bitCount) - 2 ;
}
public static void main (String[] args)
{
int n = 10 ;
System.out.println(bitwiseOrTillN(n));
}
}
|
Python3
from math import log2
def bitwiseOrTillN(n) :
if (n < 2 ) :
return 0 ;
bitCount = int (log2(n)) + 1 ;
return pow ( 2 , bitCount) - 2 ;
if __name__ = = "__main__" :
n = 10 ;
print (bitwiseOrTillN(n));
|
C#
using System;
class GFG
{
static int bitwiseOrTillN( int n)
{
if (n < 2)
return 0;
int bitCount = ( int )(Math.Log(n)/Math.Log(2)) + 1;
return ( int )Math.Pow(2, bitCount) - 2;
}
public static void Main()
{
int n = 10;
Console.WriteLine(bitwiseOrTillN(n));
}
}
|
Javascript
<script>
function bitwiseOrTillN(n)
{
if (n < 2)
return 0;
var bitCount = parseInt(Math.log2(n) + 1);
return Math.pow(2, bitCount) - 2;
}
var n = 10;
document.write( bitwiseOrTillN(n));
</script>
|
Time Complexity: O(log(log n), where n is the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...