给定两个32位数字N和M,以及两个位置i和j。编写一种将M插入N的方法,以使M从第j位开始,到第i位结束。您可以假设j到i位有足够的空间来容纳所有M。假定索引从0开始。
例子:
a) N = 1024 (10000000000),
M = 19 (10011),
i = 2, j = 6
Output : 1100 (10001001100)
b) N = 1201 (10010110001)
M = 8 (1000)
i = 3, j = 6
Output: 1217 (10011000001)
在上一篇文章中已经讨论了这个问题。在这篇文章中,讨论了一种不同的方法。
方法:这个想法很简单,下面是逐步过程–
- 捕获N中i之前的所有位,即从i-1到0。
- 我们不想更改这些位,因此我们将捕获它们并在以后使用
- 清除从j到0的所有位
- 在位置i到i的N中将M插入
- 将捕获的位插入相应的位置,即。从i-1到0
让我们尝试解决示例b,以清楚地说明该过程。
在N中捕获i-1至0位
创建一个掩码,其i-1至0位为1,其余均为0。将1移至i的左侧,并从中减去1,以获得设置了i-1至0位的位掩码。
capture_mask = ( 1 << i ) - 1
因此,例如b,遮罩将为–
capture_mask = ( 1 << 3 ) - 1
Now capture_mask = 1(001)
现在,与该具有N的掩码,i-1至0位将保持不变,而其余位变为0。因此,我们只剩下i-1至0位。
captured_bits = N & capture_mask
例如,b,捕获的位将是–
captured_bits = N & capture_mask
Now capture_mask = 1 (001)
清除N中从j到0的位
由于已捕获位,因此将位j清除为0而不丢失位i-1到0。创建一个掩码,使其j到0的位为0,其余的为1。通过在其中将-1移至j + 1来创建此掩码。左边。
clear_mask = -1 << ( j + 1 )
现在将j清除为0,并用N掩码。
N &= clear_mask
例如b将是:
N &= clear_mask
Now N = 1152 (10010000000)
在N中插入M
现在由于N已清除了从j到0的位,因此只需将M装入N并将M i的位置向左移动,以将M的MSB与N中的位置j对齐。
M <<= i
例如b-
M <<= 3
Now M = 8 << 3 = 64 (1000000)
对M与N进行“或”运算以将M插入所需的位置。
N |= M
例如b –
N |= M
Now N = 1152 | 64 = 1216 (10011000000)
将捕获的位插入N
到现在为止,已经将M插入到N中。现在剩下的唯一事情是将位置i-1处的捕获位合并回去。这可以简单地通过对N和捕获位进行OR来完成–
N |= captured_bits
例如b –
N |= captured_bits
N = 1216 | 1 = 1217 (10011000001)
因此,最终在插入之后,获得了以下位集。
N(before) = 1201 (10010110001)
N(after) = 1201 (10011000001)
下面是上述方法的实现:
C++
// C++ program to insert 32-bit number
// M into N using bit magic
#include
using namespace std;
// print binary representation of n
void bin(unsigned n)
{
if (n > 1)
bin(n / 2);
printf("%d", n % 2);
}
// Insert m into n
int insertion(int n, int m, int i, int j)
{
int clear_mask = -1 << (j + 1);
int capture_mask = (1 << i) - 1;
// Capturing bits from i-1 to 0
int captured_bits = n & capture_mask;
// Clearing bits from j to 0
n &= clear_mask;
// Shiftng m to align with n
m = m << i;
// Insert m into n
n |= m;
// Insert captured bits
n |= captured_bits;
return n;
}
// Driver Code
int main()
{
// print original bitset
int N = 1201, M = 8, i = 3, j = 6;
cout << "N = " << N << "(";
bin(N);
cout << ")"
<< "\n";
// print original bitset
cout << "M = " << M << "(";
bin(M);
cout << ")"
<< "\n";
// Call function to insert M to N
N = insertion(N, M, i, j);
cout << "After inserting M into N from 3 to 6"
<< "\n";
// Print the inserted bitset
cout << "N = " << N << "(";
bin(N);
cout << ")"
<< "\n";
return 0;
}
Java
// Java program to insert
// 32-bit number M into N
// using bit magic
import java.io.*;
class GFG
{
// print binary
// representation of n
static void bin(long n)
{
if (n > 1)
bin(n / 2);
System.out.print(n % 2);
}
// Insert m into n
static int insertion(int n, int m,
int i, int j)
{
int clear_mask = -1 << (j + 1);
int capture_mask = (1 << i) - 1;
// Capturing bits from i-1 to 0
int captured_bits = n & capture_mask;
// Clearing bits from j to 0
n &= clear_mask;
// Shiftng m to align with n
m = m << i;
// Insert m into n
n |= m;
// Insert captured bits
n |= captured_bits;
return n;
}
// Driver Code
public static void main (String[] args)
{
// print original bitset
int N = 1201, M = 8, i = 3, j = 6;
System.out.print("N = " + N + "(");
bin(N);
System.out.println(")");
// print original bitset
System.out.print("M = " + M + "(");
bin(M);
System.out.println(")");
// Call function to insert M to N
N = insertion(N, M, i, j);
System.out.println( "After inserting M " +
"into N from 3 to 6");
// Print the inserted bitset
System.out.print("N = " + N + "(");
bin(N);
System.out.println(")");
}
}
// This code is contributed
// by inder_verma.
Python3
# Python 3 program to insert 32-bit number
# M into N using bit magic
# insert M into N
def insertion(n, m, i, j):
clear_mask = -1 << (j + 1)
capture_mask = (1 << i) - 1
# Capturing bits from i-1 to 0
captured_bits = n & capture_mask
# Clearing bits from j to 0
n &= clear_mask
# Shiftng m to align with n
m = m << i
# Insert m into n
n |= m
# Insert captured bits
n |= captured_bits
return n
# Driver
def main():
N = 1201; M = 8; i = 3; j = 6
print("N = {}({})".format(N, bin(N)))
print("M = {}({})".format(M, bin(M)))
N = insertion(N, M, i, j)
print("***After inserting M into N***")
print("N = {}({})".format(N, bin(N)))
if __name__ == '__main__':
main()
C#
// C# program to insert
// 32-bit number M into N
// using bit magic
using System;
class GFG
{
// print binary
// representation of n
static void bin(long n)
{
if (n > 1)
bin(n / 2);
Console.Write(n % 2);
}
// Insert m into n
static int insertion(int n, int m,
int i, int j)
{
int clear_mask = -1 << (j + 1);
int capture_mask = (1 << i) - 1;
// Capturing bits from i-1 to 0
int captured_bits = n & capture_mask;
// Clearing bits from j to 0
n &= clear_mask;
// Shiftng m to align with n
m = m << i;
// Insert m into n
n |= m;
// Insert captured bits
n |= captured_bits;
return n;
}
// Driver Code
static public void Main (String []args)
{
// print original bitset
int N = 1201, M = 8, i = 3, j = 6;
Console.Write("N = " + N + "(");
bin(N);
Console.WriteLine(")");
// print original bitset
Console.Write("M = " + M + "(");
bin(M);
Console.WriteLine(")");
// Call function to
// insert M to N
N = insertion(N, M, i, j);
Console.WriteLine("After inserting M " +
"into N from 3 to 6");
// Print the inserted bitset
Console.Write("N = " + N + "(");
bin(N);
Console.WriteLine(")");
}
}
// This code is contributed
// by Arnab Kundu
PHP
1)
bin($n / 2);
echo $n % 2;
}
// Insert m into n
function insertion($n, $m, $i, $j)
{
$clear_mask = -1 << ($j + 1);
$capture_mask = (1 << $i) - 1;
// Capturing bits from i-1 to 0
$captured_bits = $n & $capture_mask;
// Clearing bits from j to 0
$n &= $clear_mask;
// Shiftng m to align with n
$m = $m << $i;
// Insert m into n
$n |= $m;
// Insert captured bits
$n |= $captured_bits;
return $n;
}
// Driver Code
// print original bitset
$N = 1201;
$M = 8;
$i = 3;
$j = 6;
echo "N = " . $N ."(";
bin($N);
echo ")" ."\n";
// print original bitset
echo "M = " . $M ."(";
bin($M);
echo ")". "\n";
// Call function to insert M to N
$N = insertion($N, $M, $i, $j);
echo "After inserting M into N " .
"from 3 to 6" ."\n";
// Print the inserted bitset
echo "N = " . $N . "(";
bin($N);
echo ")". "\n";
// This code is contributed
// by ChitraNayal
?>
输出:
N = 1201(10010110001)
M = 8(1000)
After inserting M into N from 3 to 6
N = 1217(10011000001)