📌  相关文章
📜  检查给定范围内的位是否处于备用模式。套装2

📅  最后修改于: 2021-05-25 09:48:53             🧑  作者: Mango

给定一个非负数N和两个值LR 。问题是检查N是否在LR的二进制表示形式中具有替代模式。

在此,交替模式表示置位和未置位以交替顺序出现。这些位从右到左编号,即,最低有效位被认为在第一位置。

例子

Input : N = 18, L = 1, R = 3
Output : Yes
(18)10 = (10010)2
The bits in the range 1 to 3 in the
binary representation of 18 are in
alternate order.

Input : N = 22, L = 2, R = 4
Output : No
(22)10 = (10110)2
The bits in the range 2 to 4 in the
binary representation of 22 are not in
alternate order.

简单方法:本文已对此进行了讨论,其最坏情况下的时间复杂度为O(log 2 n)。

高效方法:以下是步骤:

  1. 声明两个变量numleft_shift
  2. 检查n中是否设置了第r个位。请参阅这篇文章。如果设置,则分配num = n和left_shift = r,否则在n中设置第(r + 1)位并将其分配给num 。请参阅这篇文章。还要分配left_shift = r + 1。
  3. 执行num = num&(((1 << left_shift)– 1)。
  4. 执行num = num >>(l – 1)。
  5. 最后,检查是否以num的交替模式显示位。请参阅这篇文章。

上面方法的整个思想是创建一个数字num ,其中位数与n的给定范围相同,然后检查位数是否为num的交替形式。

C++
// C++ implementation to check whether bits are in
// alternate pattern in the given range
#include 
  
using namespace std;
  
// function to check whether rightmost
// kth bit is set or not in 'n'
bool isKthBitSet(unsigned int n,
                 unsigned int k)
{
    if ((n >> (k - 1)) & 1)
        return true;
    return false;
}
  
// function to set the rightmost kth bit in 'n'
unsigned int setKthBit(unsigned int n,
                       unsigned int k)
{
    // kth bit of n is being set by this operation
    return ((1 << (k - 1)) | n);
}
  
// function to check if all the bits are set or not
// in the binary representation of 'n'
bool allBitsAreSet(unsigned int n)
{
    // if true, then all bits are set
    if (((n + 1) & n) == 0)
        return true;
  
    // else all bits are not set
    return false;
}
  
// function to check if a number
// has bits in alternate pattern
bool bitsAreInAltOrder(unsigned int n)
{
    unsigned int num = n ^ (n >> 1);
  
    // to check if all bits are set
    // in 'num'
    return allBitsAreSet(num);
}
  
// function to check whether bits are in
// alternate pattern in the given range
bool bitsAreInAltPatrnInGivenRange(unsigned int n,
                                   unsigned int l,
                                   unsigned int r)
{
    unsigned int num, left_shift;
  
    // preparing a number 'num' and 'left_shift'
    // which can be further used for the check
    // of alternate pattern in the given range
    if (isKthBitSet(n, r)) {
        num = n;
        left_shift = r;
    }
    else {
        num = setKthBit(n, (r + 1));
        left_shift = r + 1;
    }
  
    // unset all the bits which are left to the
    // rth bit of (r+1)th bit
    num = num & ((1 << left_shift) - 1);
  
    // right shift 'num' by (l-1) bits
    num = num >> (l - 1);
  
    return bitsAreInAltOrder(num);
}
  
// Driver program to test above
int main()
{
    unsigned int n = 18;
    unsigned int l = 1, r = 3;
    if (bitsAreInAltPatrnInGivenRange(n, l, r))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java implementation to check whether bits are in
// alternate pattern in the given range
class GFG 
{
  
// function to check whether rightmost
// kth bit is set or not in 'n'
static boolean isKthBitSet(int n,
                            int k)
{
    if ((n >> (k - 1)) == 1)
        return true;
    return false;
}
  
// function to set the rightmost kth bit in 'n'
static int setKthBit(int n,
                    int k)
{
    // kth bit of n is being set by this operation
    return ((1 << (k - 1)) | n);
}
  
// function to check if all the bits are set or not
// in the binary representation of 'n'
static boolean allBitsAreSet(int n)
{
    // if true, then all bits are set
    if (((n + 1) & n) == 0)
        return true;
  
    // else all bits are not set
    return false;
}
  
// function to check if a number
// has bits in alternate pattern
static boolean bitsAreInAltOrder(int n)
{
    int num = n ^ (n >> 1);
  
    // to check if all bits are set
    // in 'num'
    return allBitsAreSet(num);
}
  
// function to check whether bits are in
// alternate pattern in the given range
static boolean bitsAreInAltPatrnInGivenRange(int n,
                                int l, int r)
{
    int num, left_shift;
  
    // preparing a number 'num' and 'left_shift'
    // which can be further used for the check
    // of alternate pattern in the given range
    if (isKthBitSet(n, r)) 
    {
        num = n;
        left_shift = r;
    }
    else
    {
        num = setKthBit(n, (r + 1));
        left_shift = r + 1;
    }
  
    // unset all the bits which are left to the
    // rth bit of (r+1)th bit
    num = num & ((1 << left_shift) - 1);
  
    // right shift 'num' by (l-1) bits
    num = num >> (l - 1);
  
    return bitsAreInAltOrder(num);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 18;
    int l = 1, r = 3;
    if (bitsAreInAltPatrnInGivenRange(n, l, r))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python 3 implementation to check 
# whether bits are in alternate pattern
# in the given range
  
# function to check whether rightmost
# kth bit is set or not in 'n'
def isKthBitSet(n, k):
    if((n >> (k - 1)) & 1):
        return True
    return False
  
# function to set the rightmost kth bit in 'n'
def setKthBit(n, k):
      
    # kth bit of n is being set
    # by this operation
    return ((1 << (k - 1)) | n)
  
# function to check if all the bits are set or not
# in the binary representation of 'n'
def allBitsAreSet(n):
      
    # if true, then all bits are set
    if (((n + 1) & n) == 0):
        return True
  
    # else all bits are not set
    return False
  
# function to check if a number
# has bits in alternate pattern
def bitsAreInAltOrder(n):
    num = n ^ (n >> 1)
  
    # to check if all bits are set
    # in 'num'
    return allBitsAreSet(num)
  
# function to check whether bits are in
# alternate pattern in the given range
def bitsAreInAltPatrnInGivenRange(n, l, r):
      
    # preparing a number 'num' and 'left_shift'
    # which can be further used for the check
    # of alternate pattern in the given range
    if (isKthBitSet(n, r)):
        num = n
        left_shift = r
  
    else:
        num = setKthBit(n, (r + 1))
        left_shift = r + 1
      
    # unset all the bits which are left to the
    # rth bit of (r+1)th bit
    num = num & ((1 << left_shift) - 1)
  
    # right shift 'num' by (l-1) bits
    num = num >> (l - 1)
  
    return bitsAreInAltOrder(num)
  
# Driver Code
if __name__ == '__main__':
    n = 18
    l = 1
    r = 3
    if (bitsAreInAltPatrnInGivenRange(n, l, r)):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation to check whether bits are in 
// alternate pattern in the given range 
using System;
  
class GFG 
{ 
  
// function to check whether rightmost 
// kth bit is set or not in 'n' 
static bool isKthBitSet(int n, 
                            int k) 
{ 
    if ((n >> (k - 1)) == 1) 
        return true; 
    return false; 
} 
  
// function to set the rightmost kth bit in 'n' 
static int setKthBit(int n, 
                    int k) 
{ 
    // kth bit of n is being set by this operation 
    return ((1 << (k - 1)) | n); 
} 
  
// function to check if all the bits are set or not 
// in the binary representation of 'n' 
static bool allBitsAreSet(int n) 
{ 
    // if true, then all bits are set 
    if (((n + 1) & n) == 0) 
        return true; 
  
    // else all bits are not set 
    return false; 
} 
  
// function to check if a number 
// has bits in alternate pattern 
static bool bitsAreInAltOrder(int n) 
{ 
    int num = n ^ (n >> 1); 
  
    // to check if all bits are set 
    // in 'num' 
    return allBitsAreSet(num); 
} 
  
// function to check whether bits are in 
// alternate pattern in the given range 
static bool bitsAreInAltPatrnInGivenRange(int n, 
                                int l, int r) 
{ 
    int num, left_shift; 
  
    // preparing a number 'num' and 'left_shift' 
    // which can be further used for the check 
    // of alternate pattern in the given range 
    if (isKthBitSet(n, r)) 
    { 
        num = n; 
        left_shift = r; 
    } 
    else
    { 
        num = setKthBit(n, (r + 1)); 
        left_shift = r + 1; 
    } 
  
    // unset all the bits which are left to the 
    // rth bit of (r+1)th bit 
    num = num & ((1 << left_shift) - 1); 
  
    // right shift 'num' by (l-1) bits 
    num = num >> (l - 1); 
  
    return bitsAreInAltOrder(num); 
} 
  
// Driver code 
public static void Main() 
{ 
    int n = 18; 
    int l = 1, r = 3; 
    if (bitsAreInAltPatrnInGivenRange(n, l, r)) 
        Console.WriteLine("Yes"); 
    else
        Console.WriteLine("No"); 
} 
} 
  
/* This code contributed by PrinciRaj1992 */


输出:
Yes

时间复杂度:O(1)。