Set bits in N equals to M in the given range.
You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j).
Examples :
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.
Asked in : Adobe
A simple solution is to traverse all bits in N from 0 to 31 and set the bits equals to M in the range from i to j.
An efficient solution is to do following steps.
- Set all the bits after j in a number.
- Set all the bits before i in a number.
- Then perform Bitwise Or on both then we get the number with all the bits set except from i to j.
- Perform Bitwise And with the given N as to set the bits according to the N.
- Then shift M into the correct position i.e. in the range of i to j.
- And at the last perform Bitwise Or on (Shifted M and the N modified in 4th step).
- The result will be N with M as substring from ith to jth bits
C++
#include <iostream>
using namespace std;
int setBits( 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 masked_n = n & mask;
int m_shifted = m << i;
return (masked_n | m_shifted);
}
int main()
{
int n = 2, m = 4;
int i = 2, j = 4;
cout << setBits(n, m, i, j);
return 0;
}
|
Java
public class GFG
{
static int setBits( 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 masked_n = n & mask;
int m_shifted = m << i;
return (masked_n | m_shifted);
}
public static void main(String[] args)
{
int n = 2 , m = 4 ;
int i = 2 , j = 4 ;
System.out.println(setBits(n, m, i, j));
}
}
|
Python3
def setBits(n, m, i, j):
allOnes = not 0
left = allOnes << (j + 1 )
right = (( 1 << i) - 1 )
mask = left | right
masked_n = n & mask
m_shifted = m << i
return (masked_n | m_shifted)
n, m = 2 , 4
i, j = 2 , 4
print (setBits(n, m, i, j))
|
C#
using System;
public class GFG {
static int setBits( 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 masked_n = n & mask;
int m_shifted = m << i;
return (masked_n | m_shifted);
}
public static void Main()
{
int n = 2, m = 4;
int i = 2, j = 4;
Console.WriteLine(setBits(n, m, i, j));
}
}
|
PHP
<?php
function setBits( $n , $m , $i , $j )
{
$allOnes = ~0;
$left = $allOnes << ( $j + 1);
$right = ((1 << $i ) - 1);
$mask = $left | $right ;
$masked_n = $n & $mask ;
$m_shifted = $m << $i ;
return ( $masked_n | $m_shifted );
}
$n = 2; $m = 4;
$i = 2; $j = 4;
echo setBits( $n , $m , $i , $j );
?>
|
Javascript
<script>
function setBits(n, m, i, j)
{
let allOnes = ~0;
let left = allOnes << (j + 1);
let right = ((1 << i) - 1);
let mask = left | right;
let masked_n = n & mask;
let m_shifted = m << i;
return (masked_n | m_shifted);
}
let n = 2, m = 4;
let i = 2, j = 4;
document.write(setBits(n, m, i, j));
</script>
|
Output :
18
Time Complexity: O(1)
Auxiliary Space: O(1)
Reference:
https://www.careercup.com/question?id=8863294
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...