📜  给定范围内的未设置位

📅  最后修改于: 2021-05-25 01:22:58             🧑  作者: Mango

给定一个非负数n和两个值lr 。问题是要取消设置n的二进制表示形式中范围在lr之间的位,即要取消设置从最右边的第l位到最右边的r个位的位。
约束: 1 <= l <= r <= n的二进制表示形式的位数。
例子:

Input : n = 42, l = 2, r = 5
Output : 32
(42)10 = (101010)2
(32)10 = (100000)2
The bits in the range 2 to 5 in the binary
representation of 42 have been unset.

Input : n = 63, l = 1, r = 4
Output : 48

方法:以下是步骤:

  1. 计算num =(1 <<((sizeof(int)* 8 – 1))–1。这将产生最高的正整数numnum中的所有位都将被设置。
  2. num中切换范围从lr的位。请参阅这篇文章。
  3. 现在,执行n = n&num 。这将取消设置n中从lr的范围内的位。
  4. 返回n

注意: sizeof(int)已用作int数据类型的输入。对于大型输入,可以使用long intlong long int数据类型代替int

C / C++

Java
// Java implementation to unset bits in the given range
import java.io.*;
 
class GFG
{
    // Function to toggle bits in the given range
    static int toggleBitsFromLToR(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);
  
        // toggle the bits in the range l to r in 'n'
        // and return the number
        return (n ^ num);
    }
     
    // Function to unset bits in the given range
    static int unsetBitsInGivenRange(int n, int l, int r)
    {
        // 'num' is the highest positive integer number
        // all the bits of 'num' are set
        int num = (1 << (4 * 8 - 1)) - 1;
  
        // toggle the bits in the range l to r in 'num'
        num = toggleBitsFromLToR(num, l, r);
  
        // unset the bits in the range l to r in 'n'
        // and return the number
        return (n & num);
    }
     
    // driver program
    public static void main (String[] args)
    {
        int n = 42;
        int l = 2, r = 5;
        System.out.println(unsetBitsInGivenRange(n, l, r));
    }
}
 
// Contributed by Pramod Kumar


Python3
# python implementation to unset bits
# in the given range
 
# Function to toggle bits in the
# given range
def toggleBitsFromLToR(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))
 
    # toggle the bits in the range
    # l to r in 'n' and return the
    # number
    return (n ^ num)
     
# Function to unset bits in the
# given range
def unsetBitsInGivenRange(n, l, r):
     
    # 'num' is the highest positive
    # integer number all the bits
    # of 'num' are set
    num = (1 << (4 * 8 - 1)) - 1
 
    # toggle the bits in the range
    # l to r in 'num'
    num = toggleBitsFromLToR(num, l, r)
 
    # unset the bits in the range
    # l to r in 'n' and return the
    # number
    return (n & num)
 
# Driver code   
n = 42
l = 2
r = 5
print(unsetBitsInGivenRange(n, l, r))
 
# This code is contributed by Sam007.


C#
// C#  implementation to unset
// bits in the given range
using System;
  
class GFG {
      
    // Function to toggle bits in the given range
    static int toggleBitsFromLToR(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);
  
        // toggle the bits in the
        // range l to r in 'n'
        // and return the number
        return (n ^ num);
    }
      
    // Function to unset bits in the given range
    static int unsetBitsInGivenRange(int n, int l, int r)
    {
        // 'num' is the highest
        // positive integer number
        // all the bits of 'num'
        // are set
        int num = (1 << (2 * 8 - 1)) - 1;
  
        // toggle the bits in
        // the range l to r in 'num'
        num = toggleBitsFromLToR(num, l, r);
  
        // unset the bits in
        // the range l to r in 'n'
        // and return the number
        return (n & num);
    }
      
// Driver Code
static public void Main() {
     
    int n = 42;
    int l = 2, r = 5;
    Console.WriteLine(unsetBitsInGivenRange(n, l, r));
}
}
  
// This Code is  Contributed by akt_mit


PHP


Javascript


输出:

32