📌  相关文章
📜  在给定范围内,将N中的位置1等于M。

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

您将获得两个32位数字N和M,以及两个位置i和j。编写一种方法来将N中的i和j之间的所有位设置为等于M(例如,M成为位于i处并从j开始的N的子串)。
例子 :

Input : N = 1, M = 2, i = 2, j = 4
Output: 9
N = 00000001(Considering 8 bits only)
M = 10 (Binary of 2) For more indexes,
leading zeroes will be considered.
Now set 3 bits from ith index to j in 
the N as in the M.
Bits:-    0 0 0 (0  1  0) 0 1 = 9
Indexes:- 7 6 5  4  3  2  1 0
From index 2 to 4, bits are set according 
to the M.

询问:Adobe

一个简单的解决方案是遍历N中从0到31的所有位,并在i到j的范围内设置等于M的位。
一个有效的解决方案是执行以下步骤。

  1. 将j后面的所有位设置为一个数字。
  2. 设置数字中i之前的所有位。
  3. 然后对两个都执行按位或运算,然后得到除i到j之外所有位均已设置的数字。
  4. 使用给定的N执行按位与运算,以根据N设置位。
  5. 然后将M移到正确的位置,即在i到j的范围内。
  6. 最后执行“按位或”运算(在第4步中对M和N进行移位)。
  7. 结果将是N,其中M是从第i个比特到第j个比特的子串
C++
// C++ program for above implementation
#include 
using namespace std;
 
// Function to set the bits
int setBits(int n, int m, int i, int j)
{
    // number with all 1's
    int allOnes = ~0;
 
    // Set all the bits in the left of j
    int left = allOnes << (j + 1);
 
    // Set all the bits in the right of j
    int right = ((1 << i) - 1);
 
    // Do Bitwsie OR to get all the bits
    // set except in the range from i to j
    int mask = left | right;
 
    // clear bits j through i
    int masked_n = n & mask;
 
    // move m into the correct position
    int m_shifted = m << i;
 
    // return the Bitwise OR of masked_n
    // and shifted_m
    return (masked_n | m_shifted);
}
 
// Drivers program
int main()
{
    int n = 2, m = 4;
    int i = 2, j = 4;
    cout << setBits(n, m, i, j);
    return 0;
}


Java
// Java Program
public class GFG
{
    // Function to set the bits
    static int setBits(int n, int m, int  i, int j)
    {
         
        // number with all 1's
        int  allOnes = ~0;
         
        // Set all the bits in the left of j
        int left = allOnes << (j + 1);
         
        // Set all the bits in the right of j
        int right = ((1 << i) - 1);
         
        // Do Bitwise OR to get all the bits
        // set except in the range from i to j
        int mask = left | right;
         
        // clear bits j through i
        int masked_n = n & mask;
         
        // move m into the correct position
        int m_shifted = m << i;
         
        // return the Bitwise OR of masked_n
        // and shifted_m
        return (masked_n | m_shifted);
    }
     
    // Driver Program to test above function
    public static void main(String[] args)
    {
        int n = 2, m = 4;
        int i = 2, j = 4;
        System.out.println(setBits(n, m, i, j));
    }
}
// This code is contributed by Sumit Ghosh


Python
# Python program for above implementation
 
# Function to set the bits
def setBits(n, m, i, j):
 
    # number with all 1's
    allOnes = not 0
  
    # Set all the bits in the left of j
    left = allOnes << (j + 1)
  
    # Set all the bits in the right of j
    right = ((1 << i) - 1)
  
    # Do Bitwsie OR to get all the bits
    # set except in the range from i to j
    mask = left | right
  
    # clear bits j through i
    masked_n = n & mask
  
    # move m into the correct position
    m_shifted = m << i
  
    # return the Bitwise OR of masked_n
    # and shifted_m
    return (masked_n | m_shifted)
  
# Drivers program
n, m = 2, 4
i, j = 2, 4
print setBits(n, m, i, j)
 
# This code is submitted by Sachin Bisht


C#
// C# Program for above implementation
using System;
 
public class GFG {
     
    // Function to set the bits
    static int setBits(int n, int m, int  i, int j)
    {
          
        // number with all 1's
        int  allOnes = ~0;
          
        // Set all the bits in the left of j
        int left = allOnes << (j + 1);
          
        // Set all the bits in the right of j
        int right = ((1 << i) - 1);
          
        // Do Bitwise OR to get all the bits
        // set except in the range from i to j
        int mask = left | right;
          
        // clear bits j through i
        int masked_n = n & mask;
          
        // move m into the correct position
        int m_shifted = m << i;
          
        // return the Bitwise OR of masked_n
        // and shifted_m
        return (masked_n | m_shifted);
    }
      
    // Driver Program to test above function
    public static void Main()
    {
        int n = 2, m = 4;
        int i = 2, j = 4;
         
        Console.WriteLine(setBits(n, m, i, j));
    }
}
 
// This code is contributed by Anant Agarwal.


PHP


Javascript


输出 :

18