Bitwise and (or &) of a range
Last Updated :
26 Oct, 2023
Given two non-negative long integers, x and y given x <= y, the task is to find bit-wise and of all integers from x and y, i.e., we need to compute value of x & (x+1) & … & (y-1) & y.7
Examples:
Input : x = 12, y = 15
Output : 12
12 & 13 & 14 & 15 = 12
Input : x = 10, y = 20
Output : 0
A simple solution is to traverse all numbers from x to y and do bit-wise and of all numbers in range.
Steps to implement-
- Initialize a variable res with the first number x
- Run a loop from x+1 to y
- In that loop store res & element of the loop into res
- Finally return/print res
Code-
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll andOperator(ll x, ll y)
{
ll res = x;
for ( int i = x + 1; i <= y; i++) {
res = res & i;
}
return res;
}
int main()
{
ll x = 10, y = 15;
cout << andOperator(x, y);
return 0;
}
|
Java
import java.io.*;
class GFG {
static long andOperator( long x, long y)
{
long res = x;
for ( long i = x + 1 ; i <= y; i++) {
res = res & i;
}
return res;
}
public static void main(String[] args)
{
long x = 10 , y = 15 ;
System.out.print(andOperator(x, y));
}
}
|
Python3
def and_operator(x, y):
res = x
for i in range (x + 1 , y + 1 ):
res = res & i
return res
if __name__ = = "__main__" :
x = 10
y = 15
print (and_operator(x, y))
|
C#
using System;
class GFG
{
static long AndOperator( long x, long y)
{
long res = x;
for ( long i = x + 1; i <= y; i++)
{
res = res & i;
}
return res;
}
public static void Main( string [] args)
{
long x = 10, y = 15;
Console.Write(AndOperator(x, y));
}
}
|
Javascript
function andOperator(x, y)
{
let res = x;
for (let i=x+1;i<=y;i++){
res=res&i;
}
return res;
}
let x = 10, y = 15;
console.log(andOperator(x, y));
|
Time Complexity: O(y-x-1), because we have loop from x+1 to y
Auxiliary Space: O(1), because no extra space has been used
An efficient solution is to follow following steps.
1) Find position of Most Significant Bit (MSB) in both numbers.
2) If positions of MSB are different, then result is 0.
3) If positions are same. Let positions be msb_p.
……a) We add 2msb_p to result.
……b) We subtract 2msb_p from x and y,
……c) Repeat steps 1, 2 and 3 for new values of x and y.
Example 1 :
x = 10, y = 20
Result is initially 0.
Position of MSB in x = 3
Position of MSB in y = 4
Since positions are different, return result.
Example 2 :
x = 17, y = 19
Result is initially 0.
Position of MSB in x = 4
Position of MSB in y = 4
Since positions are same, we compute 24.
We add 24 to result.
Result becomes 16.
We subtract this value from x and y.
New value of x = x - 24 = 17 - 16 = 1
New value of y = y - 24 = 19 - 16 = 3
Position of MSB in new x = 1
Position of MSB in new y = 2
Since positions are different, we return result.
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int msbPos(ll n)
{
int msb_p = -1;
while (n)
{
n = n>>1;
msb_p++;
}
return msb_p;
}
ll andOperator(ll x, ll y)
{
ll res = 0;
while (x && y)
{
int msb_p1 = msbPos(x);
int msb_p2 = msbPos(y);
if (msb_p1 != msb_p2)
break ;
ll msb_val = (1 << msb_p1);
res = res + msb_val;
x = x - msb_val;
y = y - msb_val;
}
return res;
}
int main()
{
ll x = 10, y = 15;
cout << andOperator(x, y);
return 0;
}
|
Java
class GFG {
static int msbPos( long n)
{
int msb_p = - 1 ;
while (n > 0 ) {
n = n >> 1 ;
msb_p++;
}
return msb_p;
}
static long andOperator( long x, long y)
{
long res = 0 ;
while (x > 0 && y > 0 ) {
int msb_p1 = msbPos(x);
int msb_p2 = msbPos(y);
if (msb_p1 != msb_p2)
break ;
long msb_val = ( 1 << msb_p1);
res = res + msb_val;
x = x - msb_val;
y = y - msb_val;
}
return res;
}
public static void main(String[] args)
{
long x = 10 , y = 15 ;
System.out.print(andOperator(x, y));
}
}
|
Python3
def msbPos(n):
msb_p = - 1
while (n > 0 ):
n = n >> 1
msb_p + = 1
return msb_p
def andOperator(x, y):
res = 0
while (x > 0 and y > 0 ):
msb_p1 = msbPos(x)
msb_p2 = msbPos(y)
if (msb_p1 ! = msb_p2):
break
msb_val = ( 1 << msb_p1)
res = res + msb_val
x = x - msb_val
y = y - msb_val
return res
x, y = 10 , 15
print (andOperator(x, y))
|
C#
using System;
class GFG
{
static int msbPos( long n)
{
int msb_p = -1;
while (n > 0)
{
n = n >> 1;
msb_p++;
}
return msb_p;
}
static long andOperator( long x, long y)
{
long res = 0;
while (x > 0 && y > 0)
{
int msb_p1 = msbPos(x);
int msb_p2 = msbPos(y);
if (msb_p1 != msb_p2)
break ;
long msb_val = (1 << msb_p1);
res = res + msb_val;
x = x - msb_val;
y = y - msb_val;
}
return res;
}
public static void Main()
{
long x = 10, y = 15;
Console.WriteLine(andOperator(x, y));
}
}
|
Javascript
<script>
function msbPos(n)
{
let msb_p = -1;
while (n > 0) {
n = n >> 1;
msb_p++;
}
return msb_p;
}
function andOperator(x, y)
{
let res = 0;
while (x > 0 && y > 0) {
let msb_p1 = msbPos(x);
let msb_p2 = msbPos(y);
if (msb_p1 != msb_p2)
break ;
let msb_val = (1 << msb_p1);
res = res + msb_val;
x = x - msb_val;
y = y - msb_val;
}
return res;
}
let x = 10, y = 15;
document.write(andOperator(x, y));
</script>
|
PHP
<?php
function msbPos( $n )
{
$msb_p = -1;
while ( $n > 0)
{
$n = $n >> 1;
$msb_p ++;
}
return $msb_p ;
}
function andOperator( $x , $y )
{
$res = 0;
while ( $x > 0 && $y > 0)
{
$msb_p1 = msbPos( $x );
$msb_p2 = msbPos( $y );
if ( $msb_p1 != $msb_p2 )
break ;
$msb_val = (1 << $msb_p1 );
$res = $res + $msb_val ;
$x = $x - $msb_val ;
$y = $y - $msb_val ;
}
return $res ;
}
$x = 10;
$y = 15;
echo andOperator( $x , $y );
?>
|
Time Complexity: O(log(max(x, y)))
Auxiliary Space: O(1)
More efficient solution
- Flip the LSB of b.
- And check if the new number is in range(a < number < b) or not
- if the number greater than ‘a’ again flip lsb
- if it is not then that’s the answer
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll andOperator(ll x, ll y)
{
for ( int i=0; i<( int )log2(y)+1;i++)
{
if (y <= x) {
return y;
}
if (y & (1 << i)) {
y &= ~(1UL << i);
}
}
return y;
}
int main()
{
ll x = 10, y = 15;
cout << andOperator(x, y);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int andOperator( int x, int y)
{
for ( int i = 0 ; i < (Math.log(y) / Math.log( 2 )) + 1 ;
i++) {
if (y <= x) {
return y;
}
if ((y & ( 1 << i)) != 0 ) {
y &= ~( 1 << i);
}
}
return y;
}
public static void main(String[] args)
{
int x = 10 ;
int y = 15 ;
System.out.print(andOperator(x, y));
}
}
|
Python3
import math
def andOperator(x, y):
for i in range ( int (math.log2(y) + 1 )):
if (y < = x):
return y
if (y & 1 << i):
y = y & (~( 1 <<i))
return y
x, y = 10 , 15
print (andOperator(x, y))
|
C#
using System;
class GFG {
static int andOperator( int x, int y)
{
for ( int i = 0; i < (Math.Log(y) / Math.Log(2)) + 1;
i++) {
if (y <= x) {
return y;
}
if ((y & (1 << i)) != 0) {
y &= ~(1 << i);
}
}
return y;
}
public static void Main( string [] args)
{
int x = 10;
int y = 15;
Console.Write(andOperator(x, y));
}
}
|
Javascript
function andOperator(x, y)
{
for ( var i=0; i<Math.log2(y)+1;i++)
{
if (y <= x) {
return y;
}
if (y & (1 << i)) {
y &= ~(1 << i);
}
}
return y;
}
var x = 10, y = 15;
console.log(andOperator(x, y));
|
Time Complexity: O(log(y))
Auxiliary Space: O(1)
Another Approach
We know that if a number num is a power of 2 then (num &(num – 1)) is equal to 0. So if a is less than 2^k and b is greater than or equal to 2^k, then the & of all values in between a and b should be zero as (2^k & (2^k – 1)) is equal to 0. So, if both a and b lies within the same number of bits then only answer wont be zero. Now, in every case last bit is bound to be zero because even if a and b are 2 side by side numbers last bit will be different. Similarly 2nd last bit will be zero if difference between a and b is greater than 2 and this goes on for every bit. Now, take example a = 1100(12) and b = 1111(15), then last bit should be zero of the answer. For 2nd last bit we need to check whether a/2 == b/2 because if they are equal then we know that b – a <= 2. So if a/2 and b/2 is not equal then we proceed. Now, 3rd last bit should have a difference of 4 which can be checked by a/ 4 != b/4. Hence we check every bit from last until a!=b and in every step we modify a/=2(a >> 1) and b/=2(b >> 1) to reduce a bit from end.
- Run a while loop as long as a != b and a > 0
- Right shift a by 1 and right shift b by 1
- increment shiftcount
- after while loop return left * 2^(shiftcount)
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long int
int andOperator( int a, int b) {
int shiftcount = 0;
while (a != b and a > 0) {
shiftcount++;
a = a >> 1;
b = b >> 1;
}
return int64_t(a << shiftcount);
}
int32_t main() {
int a = 10, b = 15;
cout << andOperator(a, b);
return 0;
}
|
Java
import java.util.*;
class GFG {
static long andOperator( int a, int b)
{
int shiftcount = 0 ;
while (a != b && a > 0 ) {
shiftcount++;
a = a >> 1 ;
b = b >> 1 ;
}
return ( long )(a << shiftcount);
}
public static void main(String[] args)
{
int a = 10 , b = 15 ;
System.out.println(andOperator(a, b));
}
}
|
Python3
def andOperator(a,b):
shiftcount = 0
while (a! = b and a> 0 ):
shiftcount = shiftcount + 1
a = a>> 1
b = b>> 1
return a<<shiftcount
a, b = 10 , 15
print (andOperator(a, b))
|
C#
using System;
class GFG
{
static Int64 andOperator( int a, int b)
{
int shiftcount = 0;
while (a != b && a > 0)
{
shiftcount++;
a = a >> 1;
b = b >> 1;
}
return (Int64)(a << shiftcount);
}
public static void Main( string [] args) {
int a = 10, b = 15;
Console.WriteLine(andOperator(a, b));
}
}
|
Javascript
function andOperator(a, b)
{
let shiftcount = 0;
while (a != b && a > 0) {
shiftcount++;
a = a >> 1;
b = b >> 1;
}
return (a << shiftcount);
}
let a = 10, b = 15;
console.log(andOperator(a, b));
|
Time Complexity: O(log(max(x, y)))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...