📜  将所有位设置为数字的给定范围

📅  最后修改于: 2021-05-25 02:21:24             🧑  作者: Mango

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

Input : n = 17, l = 2, r = 3
Output : 23
(17)10 = (10001)2
(23)10 = (10111)2
The bits in the range 2 to 3 in the binary
representation of 17 are set.

Input : n = 50, l = 2, r = 5
Output : 62

方法:以下是步骤:

1. Find a number 'range' that has all set
   bits in given range. And all other bits
   of this number are 0.
     range = (((1 << (l - 1)) - 1) ^ 
              ((1 << (r)) - 1));

2. Now, perform "n = n | range". This will 
   set the bits in the range from l to r 
   in n.
C++
// C++ implementation to Set bits in
// the given range
#include 
using namespace std;
 
// function to toggle bits in the given range
int setallbitgivenrange(int n, int l, int r)
{
    // calculating a number 'range' having set
    // bits in the range from l to r and all other
    // bits as 0 (or unset).
    int range = (((1 << (l - 1)) - 1) ^   
                ((1 << (r)) - 1));
 
    return (n | range);
}
 
// Driver code
int main()
{
    int n = 17, l = 2, r = 3;
    cout << setallbitgivenrange(n, l, r);
    return 0;
}


Java
// java implementation to Set bits in
// the given range
import java.util.*;
 
class GFG
{
     
    // function to toggle bits in the
    // given range
    static int setallbitgivenrange(int n,
                             int l, int r)
    {
         
        // calculating a number 'range'
        // having set bits in the range
        // from l to r and all other
        // bits as 0 (or unset).
        int range = (((1 << (l - 1)) - 1) ^
                    ((1 << (r)) - 1));
     
        return (n | range);
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 17, l = 2, r = 3;
         
        System.out.println(setallbitgivenrange(
                                      n, l, r));
    }
     
}
 
// This code is contributed by Sam007.


Python3
# Python3 implementation to Set
# bits in the given range
 
# Function to toggle bits
# in the given range
def setallbitgivenrange(n, l, r):
 
    # calculating a number 'range'
    # having set bits in the range
    # from l to r and all other
    # bits as 0 (or unset).
    range = (((1 << (l - 1)) - 1) ^
                ((1 << (r)) - 1))
 
    return (n | range)
 
# Driver code
n, l, r = 17, 2, 3
print(setallbitgivenrange(n, l, r))
 
# This code is contributed by Anant Agarwal.


C#
// C# implementation to Set
// bits in the given range
using System;
 
class GFG
{
    // function to toggle bits
    // in the given range
    static int setallbitgivenrange(int n, int l, int r)
    {
         
        // calculating a number 'range'
        // having set bits in the range
        // from l to r and all other
        // bits as 0 (or unset).
        int range = (((1 << (l - 1)) - 1) ^
                     ((1 << (r)) - 1));
     
        return (n | range);
    }
     
    // Driver code
    static void Main()
    {
        int n = 17, l = 2, r = 3;
        Console.Write(setallbitgivenrange(n, l, r));
    }
     
}
 
// This code is contributed by Sam007


PHP


Javascript


输出 :

23