📜  按位和(或&)范围

📅  最后修改于: 2021-04-30 02:57:07             🧑  作者: Mango

给定两个非负长整数,x和y给定x <= y,任务是从x和y中按位查找所有整数,即,我们需要计算x&(x + 1)& …&(y-1)和y.7
例子:

Input  : x = 12, y = 15
Output : 12 
12 & 13 & 14 & 15 = 12 

Input  : x = 10, y = 20
Output : 0 

一个简单的解决方案是遍历从x到y的所有数字,并对所有范围内的数字进行按位运算。
一个有效的解决方案是遵循以下步骤。
1)在两个数字中找到最高有效位(MSB)的位置。
2)如果MSB的位置不同,则结果为0。
3)如果位置相同。设位置为msb_p。
……a)我们将2 msb_p加到结果中。
……b)我们从x和y减去2 msb_p
……c)对x和y的新值重复步骤1、2和3。

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


输出:

8

更有效的解决方案

  1. 翻转b的LSB。
  2. 并检查新数字是否在范围内(a <数字
  3. 如果数字大于“ a”,则再次翻转lsb
  4. 如果不是,那就是答案