-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-level-bit.cpp
More file actions
421 lines (308 loc) Β· 7.21 KB
/
basic-level-bit.cpp
File metadata and controls
421 lines (308 loc) Β· 7.21 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// β‘ π’ EASY BIT MANIPULATION (Foundation Level)
// π Focus: Understand operators + basic tricks
// πΉ Core Operators (MUST KNOW)
// AND & β check bit
// OR | β set bit
// XOR ^ β toggle / difference β
// NOT ~
// Left shift << (multiply by 2)
// Right shift >> (divide by 2)
// πΉ Basic Bit Tricks
// Check if number is even/odd (n & 1) β
// Check if ith bit is set
// Set ith bit
// Clear ith bit
// Toggle ith bit
// πΉ Classic Problems
// Check power of 2 (n & (n-1)) == 0 βββ
// Count set bits (naive)
// Remove last set bit (n & (n-1)) β
// Find single number (using XOR) βββ i want to all above 15 topic to explain me in detailed with mathmatical working approaches,and also used case ,and also each topic used code in c++ complete code
// Great β this is exactly how you should build bit manipulation foundations. Iβll explain all 15 topics deeply, including:
// π‘ Mathematical intuition
// βοΈ Working logic
// π Use cases
// π» Clean C++ code
// β‘ π’ 1. AND Operator (&)
// π‘ Concept
// Bitwise AND compares each bit:
// 1 & 1 = 1
// otherwise = 0
// Example:
// 5 = 101
// 3 = 011
// -----------
// 001 = 1
// π Mathematical Insight
// Acts like bit filtering β keeps only common 1s.
// π Use Cases
// Check if a bit is ON
// Masking bits
// π» Code
// #include<bits/stdc++.h>
// using namespace std;
// int main(){
// int a = 5, b = 3;
// cout << (a & b); // Output: 1
// }
// β‘ π’ 2. OR Operator (|)
// π‘ Concept
// 1 | 0 = 1
// 1 | 1 = 1
// 0 | 0 = 0
// Example:
// 5 = 101
// 3 = 011
// -----------
// 111 = 7
// π Insight
// Acts like bit merging
// π Use Cases
// Set bits
// π» Code
// cout << (5 | 3); // 7
// β‘ π’ 3. XOR Operator (^) β
// π‘ Concept
// 1 ^ 1 = 0
// 1 ^ 0 = 1
// Example:
// 5 = 101
// 3 = 011
// -----------
// 110 = 6
// π Mathematical Properties
// a ^ a = 0
// a ^ 0 = a
// Commutative & Associative
// π Use Cases
// Find unique element
// Toggle bits
// π» Code
// cout << (5 ^ 3); // 6
// β‘ π’ 4. NOT Operator (~)
// π‘ Concept
// Flips all bits
// ~5 = -(5+1) = -6
// π Insight
// Uses 2βs complement
// π Use Cases
// Bit inversion
// π» Code
// cout << (~5); // -6
// β‘ π’ 5. Left Shift (<<)
// π‘ Concept
// n << k = n Γ 2^k
// Example:
// 5 << 1 = 10
// π Use Cases
// Fast multiplication
// π» Code
// cout << (5 << 1); // 10
// β‘ π’ 6. Right Shift (>>)
// π‘ Concept
// n >> k = n / 2^k
// π Use Cases
// Fast division
// π» Code
// cout << (8 >> 1); // 4
// β‘ π’ 7. Check Even/Odd (n & 1) β
// π‘ Concept
// Last bit determines parity:
// 0 β even
// 1 β odd
// π Why?
// Binary:
// Even β last bit = 0
// Odd β last bit = 1
// π Use Cases
// Fast parity check
// π» Code
#include<bits/stdc++.h>
using namespace std;
int main(){
int n = 7;
if(n & 1) cout << "Odd";
else cout << "Even";
}
// β‘ π’ 8. Check i-th Bit
// π‘ Concept
// (n >> i) & 1
// π Logic
// Shift ith bit to last position
// π» Code
bool isSet(int n, int i){
return (n >> i) & 1;
}
// β‘ π’ 9. Set i-th Bit
// π‘ Concept
// n | (1 << i)
// π Use Case
// Turn ON a bit
// π» Code
int setBit(int n, int i){
return n | (1 << i);
}
// β‘ π’ 10. Clear i-th Bit
// π‘ Concept
// n & ~(1 << i)
// π Logic
// Create mask with 0 at ith position
// π» Code
int clearBit(int n, int i){
return n & ~(1 << i);
}
// β‘ π’ 11. Toggle i-th Bit
// π‘ Concept
// n ^ (1 << i)
// π Use Case
// Flip bit
// π» Code
int toggleBit(int n, int i){
return n ^ (1 << i);
}
// β‘ π’ 12. Power of 2 Check βββ
// π‘ Concept
// n & (n-1) == 0
// π Why it works?
// Power of 2 has only one set bit
// Example:
// 8 = 1000
// 7 = 0111
// AND = 0000
// π» Code
bool isPowerOfTwo(int n){
return n > 0 && (n & (n - 1)) == 0;
}
// β‘ π’ 13. Count Set Bits (Naive)
// π‘ Concept
// Check each bit
// π» Code
int countBits(int n){
int count = 0;
while(n){
count += (n & 1);
n >>= 1;
}
return count;
}
// β‘ π’ 14. Remove Last Set Bit β
// π‘ Concept
// n & (n-1)
// π Why?
// Removes lowest 1 bit
// Example:
// 12 = 1100
// 11 = 1011
// AND = 1000
// π» Code
int removeLastSetBit(int n){
return n & (n - 1);
}
// β‘ π’ 15. Find Single Number (XOR) βββ
// π‘ Problem
// All numbers appear twice except one
// π‘ Idea
// a ^ a = 0
// 0 ^ x = x
// π Logic
// All duplicates cancel out
// π» Code
#include<bits/stdc++.h>
using namespace std;
int singleNumber(vector<int>& nums){
int res = 0;
for(int x : nums){
res ^= x;
}
return res;
}
int main(){
vector<int> nums = {2,2,1};
cout << singleNumber(nums); // 1
}
#include<bits/stdc++.h>
using namespace std;
/* ================= BASIC OPERATIONS ================= */
// Check even/odd using AND
bool isOdd(int n){
return n & 1;
}
// Check ith bit
bool isBitSet(int n, int i){
return (n >> i) & 1;
}
// Set ith bit
int setBit(int n, int i){
return n | (1 << i);
}
// Clear ith bit
int clearBit(int n, int i){
return n & ~(1 << i);
}
// Toggle ith bit
int toggleBit(int n, int i){
return n ^ (1 << i);
}
// Remove last set bit
int removeLastSetBit(int n){
return n & (n - 1);
}
// Count set bits (naive)
int countSetBits(int n){
int cnt = 0;
while(n){
cnt += (n & 1);
n >>= 1;
}
return cnt;
}
// Power of 2 check
bool isPowerOfTwo(int n){
return n > 0 && (n & (n - 1)) == 0;
}
// Find single number using XOR
int findSingle(vector<int>& arr){
int res = 0;
for(int x : arr){
res ^= x;
}
return res;
}
/* ================= MAIN DEMO ================= */
int main(){
int n = 10; // 1010
int i = 1;
cout << "Number: " << n << endl;
// 1. AND Operator (check bit)
cout << "Is bit " << i << " set? " << (n & (1 << i)) << endl;
// 2. OR Operator (set bit)
cout << "Set bit " << i << ": " << (n | (1 << i)) << endl;
// 3. XOR Operator (toggle bit)
cout << "Toggle bit " << i << ": " << (n ^ (1 << i)) << endl;
// 4. NOT Operator
cout << "NOT of n: " << ~n << endl;
// 5. Left Shift
cout << "n << 1 (multiply by 2): " << (n << 1) << endl;
// 6. Right Shift
cout << "n >> 1 (divide by 2): " << (n >> 1) << endl;
// 7. Even/Odd
cout << "Even or Odd: " << (isOdd(n) ? "Odd" : "Even") << endl;
// 8. Check ith bit
cout << "Check ith bit: " << isBitSet(n, i) << endl;
// 9. Set ith bit
cout << "After setting ith bit: " << setBit(n, i) << endl;
// 10. Clear ith bit
cout << "After clearing ith bit: " << clearBit(n, i) << endl;
// 11. Toggle ith bit
cout << "After toggling ith bit: " << toggleBit(n, i) << endl;
// 12. Power of 2
cout << "Is power of 2? " << (isPowerOfTwo(n) ? "Yes" : "No") << endl;
// 13. Count set bits
cout << "Number of set bits: " << countSetBits(n) << endl;
// 14. Remove last set bit
cout << "After removing last set bit: " << removeLastSetBit(n) << endl;
// 15. XOR Single Number
vector<int> arr = {2, 3, 2, 4, 4};
cout << "Single number (XOR): " << findSingle(arr) << endl;
return 0;
}