Bitwise OR (or | ) of a range
Last Updated :
06 Feb, 2023
Given two integers L and R. Determine the bitwise OR of all the integers in the range [L, R] (both inclusive).
Examples:
Input: L = 3, R = 8
Output: 15
3 | 4 | 5 | 6 | 7 | 8 = 15
Input: L = 12, R = 18
Output: 31
12 | 13 | 14 | 15 | 16 | 17 | 18 = 31
A naive approach is to traverse through all the integers between L and R and do bitwise OR of all the numbers.
An efficient approach is to follow the following steps:
- Find the position of Most Significant Bit (MSB) in both the numbers (L and R)
- If the position of both MSBs are different, set all the bits from the max(MSB1, MSB2), including this different bit, up to Oth bit i.e., add the value (1 << i) for all 0 ≤ i ≤ max(MSB1, MSB2) in the answer.
- If the position of both MSBs is the same, then
- Set this bit corresponding to MSB or add the value (1 << MSB) in the answer.
- Subtract the value (1 << MSB) from both the numbers (L and R).
- Repeat steps 1, 2, and 3.
Given below is the working of the above algorithm when L = 18 and R = 21.
L = 18, R = 21
The result is initially 0.
The position of Most Significant Bit in L = 4
Position of Most Significant Bit in R = 4
Since positions are same, add value (1 << 4) i.e. 16 to the result.
Subtract (1 << 4) from L, L becomes 2.
Subtract (1 << 4) from R, R becomes 5.
Now, Position of MSB in L is 1
Position of MSB in R is 2
Since positions are different all value (1 << i) for all
0 ≤ i ≤ max(MSB1, MSB2)
i.e. Add ((1 << 2) + (1 << 1) + (1 << 0)) = 7
Hence, final result is 16 + 7 = 23.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int MSBPosition( long long int N)
{
int msb_p = -1;
while (N) {
N = N >> 1;
msb_p++;
}
return msb_p;
}
long long int findBitwiseOR( long long int L,
long long int R)
{
long long int res = 0;
int msb_p1 = MSBPosition(L);
int msb_p2 = MSBPosition(R);
while (msb_p1 == msb_p2) {
long long int res_val = (1 << msb_p1);
res += res_val;
L -= res_val;
R -= res_val;
msb_p1 = MSBPosition(L);
msb_p2 = MSBPosition(R);
}
msb_p1 = max(msb_p1, msb_p2);
for ( int i = msb_p1; i >= 0; i--) {
long long int res_val = (1 << i);
res += res_val;
}
return res;
}
int main()
{
int L = 12, R = 18;
cout << findBitwiseOR(L, R) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MSBPosition( long N)
{
int msb_p = - 1 ;
while (N > 0 )
{
N = N >> 1 ;
msb_p++;
}
return msb_p;
}
static long findBitwiseOR( long L,
long R)
{
long res = 0 ;
int msb_p1 = MSBPosition(L);
int msb_p2 = MSBPosition(R);
while (msb_p1 == msb_p2)
{
long res_val = ( 1 << msb_p1);
res += res_val;
L -= res_val;
R -= res_val;
msb_p1 = MSBPosition(L);
msb_p2 = MSBPosition(R);
}
msb_p1 = Math.max(msb_p1,
msb_p2);
for ( int i = msb_p1; i >= 0 ; i--)
{
long res_val = ( 1 << i);
res += res_val;
}
return res;
}
public static void main (String[] args)
{
int L = 12 , R = 18 ;
System.out.println(findBitwiseOR(L, R));
}
}
|
Python3
def MSBPosition(N) :
msb_p = - 1
while (N) :
N = N >> 1
msb_p + = 1
return msb_p
def findBitwiseOR(L, R) :
res = 0
msb_p1 = MSBPosition(L)
msb_p2 = MSBPosition(R)
while (msb_p1 = = msb_p2) :
res_val = ( 1 << msb_p1)
res + = res_val
L - = res_val
R - = res_val
msb_p1 = MSBPosition(L)
msb_p2 = MSBPosition(R)
msb_p1 = max (msb_p1, msb_p2)
for i in range (msb_p1, - 1 , - 1 ) :
res_val = ( 1 << i)
res + = res_val
return res
if __name__ = = "__main__" :
L , R = 12 , 18
print (findBitwiseOR(L, R))
|
C#
using System;
class GFG
{
static int MSBPosition( long N)
{
int msb_p = -1;
while (N > 0)
{
N = N >> 1;
msb_p++;
}
return msb_p;
}
static long findBitwiseOR( long L,
long R)
{
long res = 0;
int msb_p1 = MSBPosition(L);
int msb_p2 = MSBPosition(R);
while (msb_p1 == msb_p2)
{
long res_val = (1 << msb_p1);
res += res_val;
L -= res_val;
R -= res_val;
msb_p1 = MSBPosition(L);
msb_p2 = MSBPosition(R);
}
msb_p1 = Math.Max(msb_p1,
msb_p2);
for ( int i = msb_p1; i >= 0; i--)
{
long res_val = (1 << i);
res += res_val;
}
return res;
}
public static void Main ()
{
int L = 12, R = 18;
Console.WriteLine(findBitwiseOR(L, R));
}
}
|
PHP
<?php
function MSBPosition( $N )
{
$msb_p = -1;
while ( $N )
{
$N = $N >> 1;
$msb_p ++;
}
return $msb_p ;
}
function findBitwiseOR( $L , $R )
{
$res = 0;
$msb_p1 = MSBPosition( $L );
$msb_p2 = MSBPosition( $R );
while ( $msb_p1 == $msb_p2 )
{
$res_val = (1 << $msb_p1 );
$res += $res_val ;
$L -= $res_val ;
$R -= $res_val ;
$msb_p1 = MSBPosition( $L );
$msb_p2 = MSBPosition( $R );
}
$msb_p1 = max( $msb_p1 ,
$msb_p2 );
for ( $i = $msb_p1 ; $i >= 0; $i --)
{
$res_val = (1 << $i );
$res += $res_val ;
}
return $res ;
}
$L = 12; $R = 18;
echo findBitwiseOR( $L , $R );
?>
|
Javascript
<script>
function MSBPosition(N)
{
let msb_p = -1;
while (N > 0)
{
N = N >> 1;
msb_p++;
}
return msb_p;
}
function findBitwiseOR(L, R)
{
let res = 0;
let msb_p1 = MSBPosition(L);
let msb_p2 = MSBPosition(R);
while (msb_p1 == msb_p2)
{
let res_val = (1 << msb_p1);
res += res_val;
L -= res_val;
R -= res_val;
msb_p1 = MSBPosition(L);
msb_p2 = MSBPosition(R);
}
msb_p1 = Math.max(msb_p1,
msb_p2);
for (let i = msb_p1; i >= 0; i--)
{
let res_val = (1 << i);
res += res_val;
}
return res;
}
let L = 12, R = 18;
document.write(findBitwiseOR(L, R));
</script>
|
Time Complexity: O(N), where N is the most significant bit.
Space Complexity : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...