-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProblemReplaceBitsNandM.cpp
62 lines (39 loc) · 1.08 KB
/
ProblemReplaceBitsNandM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;
// Replace bits in N by M
// You are given two 32- bit numbers, N and M 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).
// EXAMPLE:
// Input: N = 10000000000,
// M = 10101, i = 2, j = 6
// Output: N = 10001010100
// for this we have to first cleard the range of bits from range i to j
// and then or with m left shift i.
int clearRangeItoJ(int n, int i, int j) {
int ones = (~0);
int a = ones<<(j+1);
int b = (1<<i) - 1;
int mask = a|b;
int ans = n&mask;
return ans;
}
int replaceBits(int n, int m, int i, int j) {
int n_ = clearRangeItoJ(n,i,j);
int ans = n_ | (m << i);
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
int n = 15;
int i = 1, j = 4;
int m = 5;
int ans = replaceBits(n,m,i,j);
cout<<ans<<endl;
return 0;
}