-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path06_replace_bits_in_n_by_m.cpp
77 lines (60 loc) · 1.61 KB
/
06_replace_bits_in_n_by_m.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Topic: Replace Bits in N by M
You are given two 32-bits 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)
Eg: Input: N = 1024 (10000000000)
M = 21 (10101)
i = 2
j = 6
Output N = 1108 (10001010100)
*/
#include <iostream>
using namespace std;
// function to update i-th bit of a given number
int clearRangeItoJ(int n, int i, int j)
{
int ones = -1;
int first_half = ones << (j+1);
int second_half = (1<<i)-1; // pow(2,i)-1
int mask = first_half | second_half;
int ans = n & mask;
return ans;
}
// function to replace bits in N by M
int replaceBits(int n, int m, int i, int j)
{
int cleared_n = clearRangeItoJ(n, i ,j);
int mask = m << i;
int ans = cleared_n | mask;
return ans;
}
// function to drive code
int main()
{
int n,m,i,j;
cout << "Enter numbers [N & M]: ";
cin >> n >> m;
cout << "Enter bit positions [i & j]: ";
cin >> i >> j;
cout << "Number after replacing : ";
cout << replaceBits(n, m, i, j);
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter numbers [N & M] : 1024 21
Enter bit positions [i & j] : 2 6
Number after replacing : 1108
Case 2:
Enter numbers [N & M] : 15 2
Enter bit positions [i & j] : 1 3
Number after replacing : 5
Approach:
- clear bits of N from range i to j
- create a mask from m
(i.e left shift m by i bits)
- return bitwise OR of cleared_N & mask
*/