📌  相关文章
📜  将m插入n,以使m从第j位开始,到第i位结束。

📅  最后修改于: 2021-04-24 14:43:04             🧑  作者: Mango

我们给了两个数字n和m,以及两个位置i和j。从j到i开始将m的位插入n。我们可以假设从j到i的位有足够的空间来容纳所有m。也就是说,如果m = 10011,则可以假定j和i之间至少有5位。例如,您不会拥有j = 3和i = 2,因为m不能完全适合第3位和第2位。

例子 :

Input : n = 1024
        m = 19
        i = 2
        j = 6;
Output : n = 1100
Binary representations of input numbers
m in binary is (10011)2
n in binary is (10000000000)2
Binary representations of output number
(10000000000)2

Input : n = 5
        m = 3
        i = 1
        j = 2
Output : 7

算法 :

1. Clear the bits j through i in n 

2. Shift m so that it lines up with bits j through i 

3. Return Bitwise AND of m and n. 

最棘手的部分是步骤1。如何清除n中的位?我们可以戴着口罩做到这一点。该掩码将全为1,但j到i位中的0除外。我们先创建遮罩的左半部分,然后再创建右半部分,以创建此遮罩。

以下是上述方法的实现。

C++
// C++ program for implementation of updateBits()
#include 
using namespace std;
  
// Function to updateBits M insert to N.
int updateBits(int n, int m, int i, int j)
{
    /* Create a mask to clear bits i through j
      in n. EXAMPLE: i = 2, j = 4. Result
      should be 11100011. For simplicity, we'll
      use just 8 bits for the example. */
  
    int allOnes = ~0; // will equal sequence of all ls
  
    // ls before position j, then 0s. left = 11100000
    int left= allOnes << (j + 1);
  
    // l's after position i. right = 00000011
    int right = ((1 << i) - 1);
  
    // All ls, except for 0s between i and j. mask 11100011
    int mask = left | right;
  
    /* Clear bits j through i then put min there */
    int n_cleared = n & mask; // Clear bits j through i.
    int m_shifted = m << i;   // Move m into correct position.
  
    return (n_cleared | m_shifted); // OR them, and we're done!
}
  
// Driver Code
int main()
{
    int n = 1024; // in Binary N= 10000000000
    int m = 19;   // in Binary M= 10011
    int i = 2, j = 6;
  
    cout << updateBits(n,m,i,j);
  
    return 0;
}


Java
// Java program for implementation of updateBits()
  
class UpdateBits 
{
    // Function to updateBits M insert to N.
    static int updateBits(int n, int m, int i, int j)
    {
        /* Create a mask to clear bits i through j
          in n. EXAMPLE: i = 2, j = 4. Result
          should be 11100011. For simplicity, we'll
          use just 8 bits for the example. */
       
        int allOnes = ~0; // will equal sequence of all ls
       
        // ls before position j, then 0s. left = 11100000
        int left= allOnes << (j + 1);
       
        // l's after position i. right = 00000011
        int right = ((1 << i) - 1);
       
        // All ls, except for 0s between i and j. mask 11100011
        int mask = left | right;
       
        /* Clear bits j through i then put min there */
        // Clear bits j through i.
        int n_cleared = n & mask; 
        // Move m into correct position.
        int m_shifted = m << i;  
          
        // OR them, and we're done!
        return (n_cleared | m_shifted); 
    }
      
    public static void main (String[] args) 
    {
        // in Binary N= 10000000000
        int n = 1024; 
          
        // in Binary M= 10011
        int m = 19;   
          
        int i = 2, j = 6;
       
        System.out.println(updateBits(n,m,i,j));
    }
}


Python3
# Python3 program for implementation
# of updateBits()
  
# Function to updateBits M insert to N.
def updateBits(n, m, i, j):
  
    # Create a mask to clear bits i through 
    # j in n. EXAMPLE: i = 2, j = 4. Result
    # should be 11100011. For simplicity, 
    # we'll use just 8 bits for the example.
  
    # will equal sequence of all ls
    allOnes = ~0 
  
    # ls before position j,
    # then 0s. left = 11100000
    left = allOnes << (j + 1)
  
    # l's after position i. right = 00000011
    right = ((1 << i) - 1)
  
    # All ls, except for 0s between
    # i and j. mask 11100011
    mask = left | right
  
    # Clear bits j through i then put min there 
    n_cleared = n & mask
      
    # Move m into correct position.
    m_shifted = m << i 
  
    return (n_cleared | m_shifted)
  
  
# Driver Code
n = 1024 # in Binary N = 10000000000
m = 19   # in Binary M = 10011
i = 2; j = 6
print(updateBits(n, m, i, j))
  
# This code is contributed by Anant Agarwal.


C#
// C# program for implementation of 
// updateBits()
using System;
  
class GFG {
      
    // Function to updateBits M 
    // insert to N.
    static int updateBits(int n, int m, 
                           int i, int j)
    {
          
        /* Create a mask to clear bits i
          through j in n. EXAMPLE: i = 2,
          j = 4. Result should be 11100011.
          For simplicity, we'll use just 8
          bits for the example. */
        
        // will equal sequence of all ls
        int allOnes = ~0; 
        
        // ls before position j, then 0s. 
        // left = 11100000
        int left= allOnes << (j + 1);
        
        // l's after position i. 
        // right = 00000011
        int right = ((1 << i) - 1);
        
        // All ls, except for 0s between i 
        // and j. mask 11100011
        int mask = left | right;
        
        /* Clear bits j through i then put 
        min there */
        // Clear bits j through i.
        int n_cleared = n & mask; 
          
        // Move m into correct position.
        int m_shifted = m << i;  
           
        // OR them, and we're done!
        return (n_cleared | m_shifted); 
    }
       
    public static void Main() 
    {
          
        // in Binary N= 10000000000
        int n = 1024; 
           
        // in Binary M= 10011
        int m = 19;   
        int i = 2, j = 6;
        
        Console.WriteLine(updateBits(n, m, i, j));
    }
}
  
//This code is contributed by Anant Agarwal.


PHP


输出 :

1100  // in Binary (10001001100)2