Skip to content
Related Articles

Related Articles

Improve Article
Bitwise and (or &) of a range
  • Difficulty Level : Medium
  • Last Updated : 31 Mar, 2021

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




// An efficient C++ program to find bit-wise & of all
// numbers from x to y.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
 
// Find position of MSB in n. For example if n = 17,
// then position of MSB is 4. If n = 7, value of MSB
// is 3
int msbPos(ll n)
{
    int msb_p = -1;
    while (n)
    {
        n = n>>1;
        msb_p++;
    }
    return msb_p;
}
 
// Function to find Bit-wise & of all numbers from x
// to y.
ll andOperator(ll x, ll y)
{
    ll res = 0; // Initialize result
 
    while (x && y)
    {
        // Find positions of MSB in x and y
        int msb_p1 = msbPos(x);
        int msb_p2 = msbPos(y);
 
        // If positions are not same, return
        if (msb_p1 != msb_p2)
            break;
 
        // Add 2^msb_p1 to result
        ll msb_val =  (1 << msb_p1);
        res = res + msb_val;
 
        // subtract 2^msb_p1 from x and y.
        x = x - msb_val;
        y = y - msb_val;
    }
 
    return res;
}
 
// Driver code
int main()
{
    ll x = 10, y = 15;
    cout << andOperator(x, y);
    return 0;
}

Java




// An efficient Java program to find bit-wise
// & of all numbers from x to y.
class GFG {
     
    // Find position of MSB in n. For example
    // if n = 17, then position of MSB is 4.
    // If n = 7, value of MSB is 3
    static int msbPos(long n)
    {
         
        int msb_p = -1;
        while (n > 0) {
            n = n >> 1;
            msb_p++;
        }
         
        return msb_p;
    }
 
    // Function to find Bit-wise & of all
    // numbers from x to y.
    static long andOperator(long x, long y)
    {
         
        long res = 0; // Initialize result
 
        while (x > 0 && y > 0) {
             
            // Find positions of MSB in x and y
            int msb_p1 = msbPos(x);
            int msb_p2 = msbPos(y);
 
            // If positions are not same, return
            if (msb_p1 != msb_p2)
                break;
 
            // Add 2^msb_p1 to result
            long msb_val = (1 << msb_p1);
            res = res + msb_val;
 
            // subtract 2^msb_p1 from x and y.
            x = x - msb_val;
            y = y - msb_val;
        }
 
        return res;
    }
     
    // Driver code
    public static void main(String[] args)
    {
         
        long x = 10, y = 15;
         
        System.out.print(andOperator(x, y));
    }
}
 
// This code is contributed by Anant Agarwal.

Python3




# An efficient Python program to find
# bit-wise & of all numbers from x to y.
 
# Find position of MSB in n. For example
# if n = 17, then position of MSB is 4.
# If n = 7, value of MSB is 3
def msbPos(n):
 
    msb_p = -1
    while (n > 0):
     
        n = n >> 1
        msb_p += 1
     
    return msb_p
 
# Function to find Bit-wise & of
# all numbers from x to y.
def andOperator(x, y):
 
    res = 0 # Initialize result
 
    while (x > 0 and y > 0):
     
        # Find positions of MSB in x and y
        msb_p1 = msbPos(x)
        msb_p2 = msbPos(y)
 
        # If positions are not same, return
        if (msb_p1 != msb_p2):
            break
 
        # Add 2^msb_p1 to result
        msb_val = (1 << msb_p1)
        res = res + msb_val
 
        # subtract 2^msb_p1 from x and y.
        x = x - msb_val
        y = y - msb_val
 
    return res
     
# Driver code
x, y = 10, 15
print(andOperator(x, y))
 
# This code is contributed by Anant Agarwal.

C#




// An efficient C# program to find bit-wise & of all
// numbers from x to y.
using System;
 
class GFG
{
    // Find position of MSB in n.
    // For example if n = 17,
    // then position of MSB is 4.
    // If n = 7, value of MSB
    // is 3
    static int msbPos(long n)
    {
        int msb_p = -1;
        while (n > 0)
        {
            n = n >> 1;
            msb_p++;
        }
        return msb_p;
    }
     
    // Function to find Bit-wise
    // & of all numbers from x
    // to y.
    static long andOperator(long x, long y)
    {
        // Initialize result
        long res = 0;
     
        while (x > 0 && y > 0)
        {
            // Find positions of MSB in x and y
            int msb_p1 = msbPos(x);
            int msb_p2 = msbPos(y);
     
            // If positions are not same, return
            if (msb_p1 != msb_p2)
                break;
     
            // Add 2^msb_p1 to result
            long msb_val = (1 << msb_p1);
            res = res + msb_val;
     
            // subtract 2^msb_p1 from x and y.
            x = x - msb_val;
            y = y - msb_val;
        }
     
        return res;
    }
     
    // Driver code
    public static void Main()
    {
        long x = 10, y = 15;
        Console.WriteLine(andOperator(x, y));
    }
}
 
// This code is contributed by Anant Agarwal.

PHP




<?php
// An efficient C++ program
// to find bit-wise & of all
// numbers from x to y.
 
// Find position of MSB in n.
// For example if n = 17, then
// position of MSB is 4. If n = 7,
// value of MSB is 3
function msbPos($n)
{
    $msb_p = -1;
    while ($n > 0)
    {
        $n = $n >> 1;
        $msb_p++;
    }
    return $msb_p;
}
 
// Function to find Bit-wise &
// of all numbers from x to y.
function andOperator($x, $y)
{
    $res = 0; // Initialize result
 
    while ($x > 0 && $y > 0)
    {
        // Find positions of
        // MSB in x and y
        $msb_p1 = msbPos($x);
        $msb_p2 = msbPos($y);
 
        // If positions are not
        // same, return
        if ($msb_p1 != $msb_p2)
            break;
 
        // Add 2^msb_p1 to result
        $msb_val = (1 << $msb_p1);
        $res = $res + $msb_val;
 
        // subtract 2^msb_p1
        // from x and y.
        $x = $x - $msb_val;
        $y = $y - $msb_val;
    }
 
    return $res;
}
 
// Driver code
$x = 10;
$y = 15;
echo andOperator($x, $y);
 
// This code is contributed
// by ihritik
?>

Javascript




<script>
    // Javascript program to find bit-wise
// & of all numbers from x to y.
 
    // Find position of MSB in n. For example
    // if n = 17, then position of MSB is 4.
    // If n = 7, value of MSB is 3
    function msbPos(n)
    {
           
        let msb_p = -1;
        while (n > 0) {
            n = n >> 1;
            msb_p++;
        }
           
        return msb_p;
    }
   
    // Function to find Bit-wise & of all
    // numbers from x to y.
    function andOperator(x, y)
    {
           
        let res = 0; // Initialize result
   
        while (x > 0 && y > 0) {
               
            // Find positions of MSB in x and y
            let msb_p1 = msbPos(x);
            let msb_p2 = msbPos(y);
   
            // If positions are not same, return
            if (msb_p1 != msb_p2)
                break;
   
            // Add 2^msb_p1 to result
            let msb_val = (1 << msb_p1);
            res = res + msb_val;
   
            // subtract 2^msb_p1 from x and y.
            x = x - msb_val;
            y = y - msb_val;
        }
   
        return res;
    }
 
// Driver Code
        let x = 10, y = 15;
           
        document.write(andOperator(x, y));
 
// This code is contributed by avijitmondal1998.
</script>

Output: 
 



8

More efficient solution 
 

  1. Flip the LSB of b.
  2. 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

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live




My Personal Notes arrow_drop_up
Recommended Articles
Page :