📜  检查是否所有位都设置在给定范围内

📅  最后修改于: 2021-04-29 03:16:23             🧑  作者: Mango

给定一个非负数n和两个值lr 。问题是检查所有位是否都在n的二进制表示形式的lr范围内设置。
约束: 1 <= l <= r <= n的二进制表示形式的位数。
例子:

Input : n = 22, l = 2, r = 3
Output : Yes
(22)10 = (10110)2
The bits in the range 2 to 3 are all set.

Input : n = 47, l = 2, r = 5 
Output : No
(47)10 = (101111)2
The bits in the range 2 to 5 are all not set.

方法:以下是步骤:

  1. 计算num =(((1 << r)– 1)^((1 <<(l-1))– 1)。这将产生一个具有r个位数的数字num ,并且范围lr的位是唯一的设置位。
  2. 计算new_num = n&num。
  3. 如果num == new_num,则返回“是”(所有位均在给定范围内设置)。
  4. 否则返回“ No”(未在给定范围内设置所有位)。
C++
// C++ implementation to check whether all the bits
// are set in the given range or not
#include 
 
using namespace std;
 
// function to check whether all the bits
// are set in the given range or not
string allBitsSetInTheGivenRange(unsigned int n,
                                 unsigned int l, unsigned int r)
{
    // calculating a number 'num' having 'r'
    // number of bits and bits in the range l
    // to r are the only set bits
    int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
     
    // new number which will only have one or more
    // set bits in the range l to r and nowhere else
    int new_num = n & num;
     
    // if both are equal, then all bits are set
    // in the given range
    if (num == new_num)
        return "Yes";
         
    // else all bits are not set   
    return "No";   
}
 
// Driver program to test above
int main()
{
    unsigned int n = 22;
    unsigned int l = 2, r = 3;
    cout << allBitsSetInTheGivenRange(n, l, r);
    return 0;
}


Java
// Java implementation to check whether all
// the bits are set in the given range or not
class GFG {
         
    // function to check whether all the bits
    // are set in the given range or not
    static String allBitsSetInTheGivenRange(int n,
                                    int l,int r)
    {
         
        // calculating a number 'num' having 'r'
        // number of bits and bits in the range
        // l to r are the only set bits
        int num = ((1 << r) - 1) ^ ((1 <<
                                  (l - 1)) - 1);
         
        // new number which will only have one
        // or more set bits in the range l to r
        // and nowhere else
        int new_num = n & num;
         
        // if both are equal, then all bits are
        // set in the given range
        if (num == new_num)
            return "Yes";
             
        // else all bits are not set
        return "No";
    }
     
    //Driver code
    public static void main (String[] args)
    {
        int n = 22;
        int l = 2, r = 3;
         
        System.out.print(allBitsSetInTheGivenRange(
                                           n, l, r));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 implementation to check
# whether all the bits are set in
# the given range or not
 
# Function to check whether all the bits
# are set in the given range or not
def allBitsSetInTheGivenRange(n, l, r):
 
    # calculating a number 'num' having 'r'
    # number of bits and bits in the range l
    # to r are the only set bits
    num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1)
     
    # new number which will only have
    # one or more set bits in the range
    # l to r and nowhere else
    new_num = n & num
     
    # if both are equal, then all bits
    # are set in the given range
    if (num == new_num):
        return "Yes"
         
    # else all bits are not set
    return "No"
 
# Driver code
n, l, r = 22, 2, 3
print(allBitsSetInTheGivenRange(n, l, r))
 
# This code is contributed by Anant Agarwal.


C#
// C# implementation to check whether all the bits
// are set in the given range or not
using System;
 
class GFG
{
    // function to check whether all the bits
    // are set in the given range or not
    static String allBitsSetInTheGivenRange(int n,
                                       int l,int r)
    {
        // calculating a number 'num' having 'r'
        // number of bits and bits in the range l
        // to r are the only set bits
        int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
          
        // new number which will only have one or more
        // set bits in the range l to r and nowhere else
        int new_num = n & num;
          
        // if both are equal, then all bits are set
        // in the given range
        if (num == new_num)
            return "Yes";
              
        // else all bits are not set   
        return "No";   
    }
     
    //Driver code
    public static void Main ()
    {
        int n = 22;
        int l = 2, r = 3;
        Console.Write(allBitsSetInTheGivenRange(n, l, r));
    }
}
 
// This code is contributed by Anant Agarwal.


PHP


Javascript


输出:

Yes