-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path05_bit_manipulation_clear_range_of_bits.cpp
108 lines (80 loc) · 2.61 KB
/
05_bit_manipulation_clear_range_of_bits.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Topic: Bit Manipulation - Clear Range of Bits
- Clearing last n bits
- Clearing bits between i to j
*/
#include <iostream>
using namespace std;
// function to clear last n bits
int clearLastBit(int n, int i)
{
int ones = ~(0);
int mask = ones << i;
int ans = (n & mask);
return ans;
/*
Approach:
- create the required mask: left shift i-bits from all ones (i.e ~(0) or -1)
- return bitwise AND of number & mask.
Eg: n = 31 (00011111)
i = 3
mask = 11111000 (i.e ~(00000000) << 3) => (11111111 << 3) => 11111000)
00011111 (n=31)
& 11111000 (mask)
-----------
00011000 (Output=24)
*/
}
// function to clear bits between range i to j
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;
/*
Approach
- Create the first & second parts of mask
a. first half (all ones after j): left shift j+1 bits from all ones
b. second half (all ones upto i): pow(2,i)-1 (Eg:1,3,7,15...)
- creating the mask : bitwise OR of first & second part of mask
- return Bitwise AND of number & mask
Eg: n = 31 (00011111)
i = 1
j = 3
first_half = 11110000 (i.e -1 << (3+1)) => (11111111 << 4) => 11110000)
second_half = 00000001 (i.e pow(2,i) - 1) => (1 << i) - 1 => 00000001 )
mask = 11110001 (i.e 11110000 | 00000001)
00011111 (n=31)
& 11110001 (mask)
------------
00010001 (Output=17)
*/
}
// function to drive code
int main()
{
int n,i,j;
cout << "Enter number: ";
cin >> n;
// Clearing last n bits
cout << "How many last bits do you want to clear: ";
cin >> i;
cout << "Number "<< n << " after clearing last " << i << "-bits : " << clearLastBit(n,i) << "\n\n";
// Clearing bits between i to j
cout << "Enter Range [i & j]: ";
cin >> i >> j;
cout << "Number "<< n << " after clearing bits from " << i << " to " << j << " : " << clearRangeItoJ(n,i,j);
cout << endl;
return 0;
}
/*
OUTPUT:
Enter number: 31
How many last bits do you want to clear: 3
Number 31 after clearing last 3-bits : 24
Enter Range [i & j]: 1 3
Number 31 after clearing bits from 1 to 3 : 17
*/