We are given two numbers n and m, and two-bit positions, i and j. Insert bits of m into n starting from j to i. We can assume that the bits j through i have enough space to fit all of m. That is, if m = 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j = 3 and i = 2, because m could not fully fit between bit 3 and bit 2.
Examples :
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
Algorithm :
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.
The trickiest part is Step 1. How do we clear the bits in n? We can do this with a mask. This mask will have all 1s, except for 0s in the bits j through i. We create this mask by creating the left half of the mask first, and then the right half.
Following is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int updateBits( int n, int m, int i, int j)
{
int allOnes = ~0;
int left= allOnes << (j + 1);
int right = ((1 << i) - 1);
int mask = left | right;
int n_cleared = n & mask;
int m_shifted = m << i;
return (n_cleared | m_shifted);
}
int main()
{
int n = 1024;
int m = 19;
int i = 2, j = 6;
cout << updateBits(n,m,i,j);
return 0;
}
|
Java
class UpdateBits
{
static int updateBits( int n, int m, int i, int j)
{
int allOnes = ~ 0 ;
int left= allOnes << (j + 1 );
int right = (( 1 << i) - 1 );
int mask = left | right;
int n_cleared = n & mask;
int m_shifted = m << i;
return (n_cleared | m_shifted);
}
public static void main (String[] args)
{
int n = 1024 ;
int m = 19 ;
int i = 2 , j = 6 ;
System.out.println(updateBits(n,m,i,j));
}
}
|
Python3
def updateBits(n, m, i, j):
allOnes = ~ 0
left = allOnes << (j + 1 )
right = (( 1 << i) - 1 )
mask = left | right
n_cleared = n & mask
m_shifted = m << i
return (n_cleared | m_shifted)
n = 1024
m = 19
i = 2 ; j = 6
print (updateBits(n, m, i, j))
|
C#
using System;
class GFG {
static int updateBits( int n, int m,
int i, int j)
{
int allOnes = ~0;
int left= allOnes << (j + 1);
int right = ((1 << i) - 1);
int mask = left | right;
int n_cleared = n & mask;
int m_shifted = m << i;
return (n_cleared | m_shifted);
}
public static void Main()
{
int n = 1024;
int m = 19;
int i = 2, j = 6;
Console.WriteLine(updateBits(n, m, i, j));
}
}
|
PHP
<?php
function updateBits( $n , $m , $i , $j )
{
$allOnes = ~0;
$left = $allOnes << ( $j + 1);
$right = ((1 << $i ) - 1);
$mask = $left | $right ;
$n_cleared = $n & $mask ;
$m_shifted = $m << $i ;
return ( $n_cleared | $m_shifted );
}
$n = 1024;
$m = 19;
$i = 2;
$j = 6;
echo updateBits( $n , $m , $i , $j );
?>
|
Javascript
<script>
function updateBits(n,m,i,j)
{
let allOnes = ~0;
let left= allOnes << (j + 1);
let right = ((1 << i) - 1);
let mask = left | right;
let n_cleared = n & mask;
let m_shifted = m << i;
return (n_cleared | m_shifted);
}
let n = 1024;
let m = 19;
let i = 2, j = 6;
document.write(updateBits(n,m,i,j));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Nov, 2022
Like Article
Save Article