📌  相关文章
📜  检查所有位是否在给定范围内未设置

📅  最后修改于: 2021-04-29 09:21:27             🧑  作者: Mango

给定一个非负数n和两个值lr 。问题是检查在n的二进制表示形式中的lr范围内,所有位是否都未设置。这些位从右到左编号,即,最低有效位被认为在第一位置。
约束: 1 <= l <= r <= n的二进制表示形式的位数。

例子:

Input : n = 17, l = 2, r = 4
Output : Yes
(17)10 = (10001)2
The bits in the range 2 to 4 are all unset.

Input : n = 39, l = 4, r = 6
Output : No
(39)10 = (100111)2
The bits in the range 4 to 6 are all not unset.

方法:以下是步骤:

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


Java
// Java implementation to check 
// whether all the bits are 
// unset in the given range or not
class GFG
{
      
// function to check whether 
// all the bits are unset in
// the given range or not
static boolean 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 could only 
    // have one or more set bits in
    // the range l to r and nowhere else
    int new_num = n & num;
  
    // if true, then all bits are 
    // unset in the given range
    if (new_num == 0)
        return true;
  
    // else all bits are not 
    // unset in the given range
    return false;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 17;
    int l = 2, r = 4;
    if (allBitsSetInTheGivenRange(n, l, r))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed
// by Smitha


Python3
# Python3 implementation to 
# check whether all the bits
# are unset in the given 
# range or not
  
# function to check whether 
# all the bits are unset 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 could only 
    # have one or more set bits in 
    # the range l to r and nowhere else
    new_num = n & num
  
    # if true, then all bits are 
    # unset in the given range
    if (new_num == 0):
        return True
  
    # else all bits are not 
    # unset in the given range
    return false
  
# Driver Code
n = 17
l = 2
r = 4
if (allBitsSetInTheGivenRange(n, l, r)):
    print("Yes")
else:
    print("No")
  
# This code is contributed 
# by Smitha


C#
// C# implementation to check 
// whether all the bits are 
// unset in the given range or not
using System;
  
class GFG
{
      
// function to check whether 
// all the bits are unset in
// the given range or not
static bool 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 could  
    // only have one or more 
    // set bits in the range 
    // l to r and nowhere else
    int new_num = n & num;
  
    // if true, then all 
    // bits are unset
    // in the given range
    if (new_num == 0)
        return true;
  
    // else all bits are not 
    // unset in the given range
    return false;
}
  
// Driver Code
public static void Main()
{
    int n = 17;
    int l = 2, r = 4;
    if (allBitsSetInTheGivenRange(n, l, r))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed 
// by Smitha


PHP


输出:

Yes