-
Notifications
You must be signed in to change notification settings - Fork 10
/
04_bit_manipulation_clear_n_update.cpp
121 lines (90 loc) · 2.77 KB
/
04_bit_manipulation_clear_n_update.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
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Topic: Bit Manipulation - Clear & Update Bits
Bitwise Operations
- Clear the i-th bit
(Given a number n, clear a particular bit in that number from the i-th position)
- Update the i-th bit
*/
#include <iostream>
using namespace std;
// function to clear i-th bit of a given number
int clearBit(int n, int i)
{
int mask = ~(1 << i); // mask = ~(Left shift i bits)
int ans = (n & mask);
/*
Bitwise AND of n and mask,
Eg: n = 5 (0101)
i = 2
mask = 1011 (i.e ~(0001 << 2) => ~(0100) => 1011)
0101 (5)
& 1011 (mask)
--------
0001 (1)
*/
return ans;
}
// function to update i-th bit of a given number
int updateBit(int n, int i, int bit_val)
{
int mask = ~(1 << i); // mask = Left shift i bits
int cleared_n = (n & mask);
int val_mask = bit_val << i;
int ans = cleared_n | val_mask;
/*
- first clear the i-th bit
- then Bitwise OR of cleared_number and val_mask
Eg: n = 5 (0101)
i = 2
mask = 1011 (i.e ~(0001 << 2) => ~(0100) => 1011)
0101 (5)
& 1011 (mask)
--------
0001 (1)
Now, cleared_n = 1 (0001)
bit_val = 0
val_mask = 0000 (i.e (0000 << 2) => 0000)
0001 (cleared_n)
| 0000 (bit_val << i)
--------
0001 (1)
*/
return ans;
}
// function to drive code
int main()
{
int n, i;
cout << "Enter number: ";
cin >> n;
// clear the i-th bit
cout << "Clear Bit (Enter the i-th bit you want to clear): ";
cin >> i;
cout << "number " << n << " after clearing " << i <<"th-bit is: " << clearBit(n,i) << "\n\n";
// Update the i-th bit
cout << "Update Bit (Enter the i-th bit you want to update): ";
cin >> i;
int bit_val;
cout << "Enter bit value for update: ";
cin >> bit_val;
cout << "number " << n << " after updating " << i <<"th-bit with " << bit_val << " is: " << updateBit(n,i, bit_val);
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter number: 5
Clear Bit (Enter the i-th bit you want to clear): 2
number 5 after clearing 0th-bit is: 1
Update Bit (Enter the i-th bit you want to update): 2
Enter bit value for update: 0
number 5 after updating 2th-bit with 0 is: 1
Case 2:
Enter number: 5
Clear Bit (Enter the i-th bit you want to clear): 0
number 5 after clearing 2th-bit is: 4
Update Bit (Enter the i-th bit you want to update): 2
Enter bit value for update: 1
number 5 after updating 2th-bit with 1 is: 5
*/