给出了两个32位数字N和M,以及两个位位置i和j。编写一个方法,将N中i和j之间的所有位设置为M(例如,M成为位于i的N的子串,从j开始)。 例如:
null
Input : N = 1, M = 2, i = 2, j = 4Output: 9N = 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 = 9Indexes:- 7 6 5 4 3 2 1 0From index 2 to 4, bits are set according to the M.
提问:Adobe
一个简单的解决方案是遍历N中从0到31的所有位,并将i到j范围内的位设置为等于M。 一个有效的解决方案是执行以下步骤。
- 设置数字中j之后的所有位。
- 在我输入一个数字之前设置所有位。
- 然后按位或同时按这两种方式执行,然后我们得到除i到j之外的所有位都已设置的数字。
- 使用给定的N执行按位与,以根据N设置位。
- 然后将M换到正确的位置,即在i到j的范围内。
- 最后执行按位或on(在第4步中移位M和修改N)。
- 结果将是N,M作为从第i位到第j位的子串
C++
// C++ program for above implementation #include <iostream> 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 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); } // 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 |
Python3
# 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 Bitwise 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
<?php // PHP program for above implementation // Function to set the bits function setBits( $n , $m , $i , $j ) { // number with all 1's $allOnes = ~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 Bitwise 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 ); } // Driver Code $n = 2; $m = 4; $i = 2; $j = 4; echo setBits( $n , $m , $i , $j ); // This code is contributed by ajit ?> |
Javascript
<script> // Javascript Program for above implementation // Function to set the bits function setBits(n, m, i, j) { // number with all 1's let allOnes = ~0; // Set all the bits in the left of j let left = allOnes << (j + 1); // Set all the bits in the right of j let right = ((1 << i) - 1); // Do Bitwise OR to get all the bits // set except in the range from i to j let mask = left | right; // clear bits j through i let masked_n = n & mask; // move m into the correct position let m_shifted = m << i; // return the Bitwise OR of masked_n // and shifted_m return (masked_n | m_shifted); } let n = 2, m = 4; let i = 2, j = 4; document.write(setBits(n, m, i, j)); </script> |
输出:
18
参考: https://www.careercup.com/question?id=8863294 本文由 萨希尔·查布拉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END