-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitwise_ops.hpp
592 lines (484 loc) · 18.6 KB
/
bitwise_ops.hpp
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#ifndef __BITWISE_OPS_H
#define __BITWISE_OPS_H
#include <iostream>
#include <iomanip>
#include <ios>
#include <bitset>
using namespace std;
/*
- Bitwise operators:
- Operators used in programming languages to perform operations at the bit level of binary numbers.
- They manipulate individual bits within a binary representation of data.
- Manipulating individual bits in a bit vector or bit flags.
- Performing efficient bitwise operations for specific algorithms or data structures.
- Extracting or manipulating specific bit patterns within binary data.
- Packing and unpacking data in memory-constrained environments.
- Optimizing code for performance or memory usage.
- These bitwise operators are often used in low-level programming languages like C, C++, and assembly language to work with individual bits, manipulate binary data, and perform optimizations in certain algorithms.
- They can be useful in tasks like bitwise masking, bit-level operations on flags and permissions, and efficient storage of data.
*/
void bitwise_ops()
{
// Size of (unsigned short int) is: 2 bytes <==> 16 bits
unsigned short int data { 0b10110010 };
std::cout << "data in dec: " << std::dec << data << std::endl;
std::cout << "data in hex: " << std::hex << std::showbase << std::uppercase << data << std::endl;
/*
- Using std::bitset<base>(data) to represent the binary on the screen
- (base): should be the Number of bits that (data) represents:
- 2 bytes <==> 16 bits
- 4 bytes <==> 32 bits
- 8 bytes <==> 64 bits
*/
std::cout << "data in bin: " << std::bitset<16>(data) << std::endl;
}
/*
- Bitwise shift operators:
- Bit shifting is only supported for integral types like int, char,...
- If data is lost as a result of you shifting bits left(<<) or right(>>), you can’t get the data back just by doing the reverse operation.
- You’ve just lost the data permanently!
- The Rules of bit shifting:
- Shifting To Right Divides by (2^n)
- Shifting To Left multiplies by (2^n)
- The Rule breaks if you throw off 1's either to the right or the left
*/
void shift_ops()
{
unsigned short int value (0x157c); // 5500
std::cout << "Original Value is: " << value << std::endl;
std::cout << "Original Value as a bitset is: " << std::bitset<16>(value) << std::endl;
std::cout << std::endl;
// Using shift ops
// using implicit cast for value by auto
auto val = (value >> 1);
// using explicit cast for value by static_cast
// 001)
// = 2750 after shift to the right 1 pos
// value = static_cast<unsigned short int>(value >> 1); // Shift 1 pos to the right.
// std::cout << "Original Value After Shift 1 position to the right is: " << value << std::endl;
// std::cout << "Original Value After Shift 1 position to the right using bitset representation is: " << std::bitset<16>(value) << std::endl;
// 002)
// = 11000 after shift to the left 1 pos
// value = static_cast<unsigned short int>(value << 1); // Shift 1 pos to the left.
// std::cout << "Original Value After Shift 1 position to the left is: " << value << std::endl;
// std::cout << "Original Value After Shift 1 position to the left using bitset representation is: " << std::bitset<16>(value) << std::endl;
// 003)
// = 44000 after shift to the left 3 pos
value = static_cast<unsigned short int>(value << 3); // Shift 3 pos to the left.
std::cout << "Original Value After Shift 3 position to the left is: " << value << std::endl;
std::cout << "Original Value After Shift 3 position to the left using bitset representation is: " << std::bitset<16>(value) << std::endl;
}
/*
- Logical Bitwise operators:
- (&) AND, (|) OR, (~) NOT, (^) XOR
- Table of truth:
--------------------------------------------------
(a) | (b) | (a&b) | (a|b) | (~a) | (a^b)
--------------------------------------------------
0 0 0 0 1 0
0 1 0 1 1 1
1 0 0 1 0 1
1 1 1 1 0 0
--------------------------------------------------
*/
void logical_bitwise_ops()
{
constexpr int WIDTH(20);
unsigned char ch01 {0x6}; // 0000 0110
unsigned char ch02 {0x9}; // 0000 1001
std::cout << "Original Values:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch01 is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch01)
<< std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch02 is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch02)
<< std::endl;
std::cout << std::endl;
// Using logical bitwise ops:
std::cout << "Bitwise (&) AND:" << std::endl;
// 001) (&) AND
std::cout
<< std::setw(WIDTH)
<< "ch01 & ch02 is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch01 & ch02)
<< std::endl;
std::cout << std::endl;
std::cout << "Bitwise (|) OR:" << std::endl;
// 002) (|) OR
std::cout
<< std::setw(WIDTH)
<< "ch01 | ch02 is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch01 | ch02)
<< std::endl;
std::cout << std::endl;
std::cout << "Bitwise (~) NOT:" << std::endl;
// 003) (~) NOT
std::cout
<< std::setw(WIDTH)
<< "~ch01 is: "
<< std::setw(WIDTH)
<< std::bitset<8>(~ch01)
<< std::endl;
std::cout
<< std::setw(WIDTH)
<< "~ch02 is: "
<< std::setw(WIDTH)
<< std::bitset<8>(~ch02)
<< std::endl;
// Using bin literal
std::cout
<< std::setw(WIDTH)
<< "~0b10101101 is: "
<< std::setw(WIDTH)
<< std::bitset<8>(~0b10101101)
<< std::endl;
// Using hex literal
std::cout
<< std::setw(WIDTH)
<< "~0xAD is: "
<< std::setw(WIDTH)
<< std::bitset<8>(~0xAD)
<< std::endl;
std::cout << std::endl;
std::cout << "Bitwise (^) XOR :" << std::endl;
// 004) (^) XOR
std::cout
<< std::setw(WIDTH)
<< "ch01 ^ ch02 is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch01 ^ ch02)
<< std::endl;
}
/*
- Compound Bitwith operators:
- They work on the variable and put the result back in the same variable again.
- (>>=) Shift To The Right With Assignment
- (<<=) Shift To The Left With Assignment
- (&=) AND To The Right With Assignment
- (|=) OR With Assignment
- (^=) XOR To The Right With Assignment
*/
void compound_bitwise_ops()
{
constexpr int WIDTH(40);
unsigned char ch = 0b10110010; // 178
std::cout << "Original Values:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl;
std::cout << std::endl;
// Using Compound Bitwith operators:
// 001) (<<=) Compound Left Shift
std::cout << "Bitwise Compound (<<=) Compound Left Shift:" << std::endl;
ch <<= 2;
std::cout
<< std::setw(WIDTH)
<< "ch <<= 2 [Compound Left Shift] is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl;
// 002) (>>=) Compound Right Shift
std::cout << std::endl;
std::cout << "Bitwise Compound (>>=) Compound Right Shift:" << std::endl;
ch >>= 2;
std::cout
<< std::setw(WIDTH)
<< "ch >>= 2 [Compound Right Shift] is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl;
// 003) (&=) Compound & AND
std::cout << std::endl;
std::cout << "Bitwise Compound (&=) Compound & AND:" << std::endl;
ch &= 0b10101101; // 173
std::cout
<< std::setw(WIDTH)
<< "ch &= 2 [Compound (&=) AND] is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl;
// 004) (|=) Compound & OR
std::cout << std::endl;
std::cout << "Bitwise Compound (|=) Compound | OR:" << std::endl;
ch |= 0b10101101; // 173
std::cout
<< std::setw(WIDTH)
<< "ch |= 2 [Compound (|=) OR] is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl;
// 005) (^=) Compound & XOR
std::cout << std::endl;
std::cout << "Bitwise Compound (^=) Compound | XOR:" << std::endl;
ch ^= 0b10101101; // 173
std::cout
<< std::setw(WIDTH)
<< "ch ^= 2 [Compound (^=) XOR] is: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl;
}
/*
- Bit Masks (Shadow):
- Operations:
- Set bit position (var |= mask)
- Reset bit position (var &= mask) OR (var &= ~(Reverse_mask))
- Check bit position ((var & mask) >> pos)
- Toggle bit position (var ^(mask))
*/
void bitwise_mask()
{
const int WIDTH { 20 };
// 8-bit = 1-byte
const unsigned char mask_bit_0 {0b00000001}; // Bit 0
const unsigned char mask_bit_1 {0b00000010}; // Bit 1
const unsigned char mask_bit_2 {0b00000100}; // Bit 2
const unsigned char mask_bit_3 {0b00001000}; // Bit 3
const unsigned char mask_bit_4 {0b00010000}; // Bit 4
const unsigned char mask_bit_5 {0b00100000}; // Bit 5
const unsigned char mask_bit_6 {0b01000000}; // Bit 6
const unsigned char mask_bit_7 {0b10000000}; // Bit 7
unsigned char ch { 0b00000000 }; // All bits starts as off
std::cout << "Original Value of ch:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
// Set bits by: |= with mask of the bits
// Targeting position 1 = bit 1 => mask_bit_1
ch |= mask_bit_1;
std::cout << "Value of ch after setting:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
// Targeting position 5 = bit 5 => mask_bit_5
ch |= mask_bit_5;
std::cout << "Value of ch after setting:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
// Targeting all positions 0-7 to set all
ch |=
(
mask_bit_0 | mask_bit_1 | mask_bit_2 | mask_bit_3 |
mask_bit_4 | mask_bit_5 | mask_bit_6 | mask_bit_7
);
std::cout << "Value of ch after setting:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
// Reset bits by: &= with mask of the bits
// Targeting position 1 = bit 1 => mask_bit_1
ch &= ~mask_bit_1;
std::cout << "Value of ch after resetting:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
// Targeting position 5 = bit 5 => mask_bit_5
ch &= ~mask_bit_5;
std::cout << "Value of ch after resetting:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
// Targeting positions 0, 2, 4, 6 to reset them
ch &=
~(
mask_bit_0 | mask_bit_2 | mask_bit_4 | mask_bit_6
);
std::cout << "Value of ch after resetting:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
// Check bits by: (& mask) >> pos
// Check the state of the bits
std::cout << "Check the state of a bit is on/off" << std::endl;
std::cout << "bit_0 is: " << ( ( ch & mask_bit_0 ) >> 0 ) << std::endl;
std::cout << "bit_0 with static_cast is: " << ( static_cast<bool>( ch & mask_bit_0 ) ) << std::endl;
std::cout << "bit_1 is: " << ( ( ch & mask_bit_1 ) >> 1 ) << std::endl;
std::cout << "bit_1 with static_cast is: " << ( static_cast<bool>( ch & mask_bit_1 ) ) << std::endl;
std::cout << "bit_2 is: " << ( ( ch & mask_bit_2 ) >> 2 ) << std::endl;
std::cout << "bit_2 with static_cast is: " << ( static_cast<bool>( ch & mask_bit_2 ) ) << std::endl;
std::cout << "bit_3 is: " << ( ( ch & mask_bit_3 ) >> 3 ) << std::endl;
std::cout << "bit_3 with static_cast is: " << ( static_cast<bool>( ch & mask_bit_3 ) ) << std::endl;
std::cout << "bit_4 is: " << ( ( ch & mask_bit_4 ) >> 4 ) << std::endl;
std::cout << "bit_4 with static_cast is: " << ( static_cast<bool>( ch & mask_bit_4 ) ) << std::endl;
std::cout << "bit_5 is: " << ( ( ch & mask_bit_5 ) >> 5 ) << std::endl;
std::cout << "bit_5 with static_cast is: " << ( static_cast<bool>( ch & mask_bit_5 ) ) << std::endl;
std::cout << "bit_6 is: " << ( ( ch & mask_bit_6 ) >> 6 ) << std::endl;
std::cout << "bit_6 with static_cast is: " << ( static_cast<bool>( ch & mask_bit_6 ) ) << std::endl;
std::cout << "bit_7 is: " << ( ( ch & mask_bit_7 ) >> 7 ) << std::endl;
std::cout << "bit_7 with static_cast is: " << ( static_cast<bool>( ch & mask_bit_7 ) ) << std::endl;
// Toggling bits by: ^(mask)
// Targeting position 0
std::cout << std::endl;
ch ^= mask_bit_0;
std::cout << "Value of ch after Toggling: " << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
// Targeting position 7
std::cout << std::endl;
ch ^= mask_bit_7;
std::cout << "Value of ch after Toggling: " << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
// Targeting positions 4, 5, 6, 7 to Toggle them
ch ^=
(
mask_bit_4 | mask_bit_5 | mask_bit_6 | mask_bit_7
);
std::cout << "Value of ch after Toggling:" << std::endl;
std::cout
<< std::setw(WIDTH)
<< "ch: "
<< std::setw(WIDTH)
<< std::bitset<8>(ch)
<< std::endl
<< std::endl;
}
/*
- Masks Demo 01
- fun_opts_v0 : No Performance / Waste Of Memory
- fun_opts_v1 : Performance / No Waste Of Memory
*/
void fun_opts_v0 // Uses many parameters & waste of memory
(
// using 1 byte for each parameter = 8 bit * 8 = 64 bit;
bool f00, // bool = 1 Byte = 8 bit.
bool f01, // bool = 1 Byte = 8 bit.
bool f02, // bool = 1 Byte = 8 bit.
bool f03, // bool = 1 Byte = 8 bit.
bool f04, // bool = 1 Byte = 8 bit.
bool f05, // bool = 1 Byte = 8 bit.
bool f06, // bool = 1 Byte = 8 bit.
bool f07 // bool = 1 Byte = 8 bit.
)
{
std::cout << std::boolalpha;
std::cout << "f00 is: " << f00 << std::endl;
std::cout << "f01 is: " << f01 << std::endl;
std::cout << "f02 is: " << f02 << std::endl;
std::cout << "f03 is: " << f03 << std::endl;
std::cout << "f04 is: " << f04 << std::endl;
std::cout << "f05 is: " << f05 << std::endl;
std::cout << "f06 is: " << f06 << std::endl;
std::cout << "f07 is: " << f07 << std::endl;
}
// Masks 8-bit = 1-byte
const unsigned char mask_bit_0 {0b00000001}; // Bit 0
const unsigned char mask_bit_1 {0b00000010}; // Bit 1
const unsigned char mask_bit_2 {0b00000100}; // Bit 2
const unsigned char mask_bit_3 {0b00001000}; // Bit 3
const unsigned char mask_bit_4 {0b00010000}; // Bit 4
const unsigned char mask_bit_5 {0b00100000}; // Bit 5
const unsigned char mask_bit_6 {0b01000000}; // Bit 6
const unsigned char mask_bit_7 {0b10000000}; // Bit 7
void fun_opts_v1 // Uses 1 parameter & no waste of memory
(
// using 1 byte parameter = 8 bit * 1 = 8 bit;
unsigned char flags // char = 1 byte
)
{
std::cout << std::boolalpha;
std::cout << "f00 is: " << ( ( flags & mask_bit_0 ) >> 0 ) << std::endl;
std::cout << "f01 is: " << ( ( flags & mask_bit_1 ) >> 1 ) << std::endl;
std::cout << "f02 is: " << ( ( flags & mask_bit_2 ) >> 2 ) << std::endl;
std::cout << "f03 is: " << ( ( flags & mask_bit_3 ) >> 3 ) << std::endl;
std::cout << "f04 is: " << ( ( flags & mask_bit_4 ) >> 4 ) << std::endl;
std::cout << "f05 is: " << ( ( flags & mask_bit_5 ) >> 5 ) << std::endl;
std::cout << "f06 is: " << ( ( flags & mask_bit_6 ) >> 6 ) << std::endl;
std::cout << "f07 is: " << ( ( flags & mask_bit_7 ) >> 7 ) << std::endl;
}
void bitwise_mask_demo_1()
{
std::cout << "fun_opts_v0: " << std::endl;
fun_opts_v0(0, 0, 1, 1, 1, 0, 1, 0);
std::cout << std::endl;
std::cout << "fun_opts_v1: " << std::endl;
fun_opts_v1(0b00111010);
std::cout << std::endl;
std::cout << "fun_opts_v1: " << std::endl;
fun_opts_v1(mask_bit_6 | mask_bit_4 | mask_bit_2 | mask_bit_0);
}
/*
- Masks Demo 02
- Packing colors information
*/
const unsigned int r_mask { 0xFF000000 }; // will be shift 24 pos
const unsigned int g_mask { 0x00FF0000 }; // will be shift 16 pos
const unsigned int b_mask { 0x0000FF00 }; // will be shift 8 pos
const unsigned int a_mask { 0x000000FF }; // will be shift 0 pos
void bitwise_mask_demo_02()
{
const int WIDTH { 20 };
unsigned int my_rgba_color { 0xAABCDE00 };
std::cout << "Colors Information: " << std::endl;
std::cout << std::uppercase << std::hex << std::showbase;
std::cout
<< std::setw(WIDTH) << "Red is: "
<< ( ( my_rgba_color & r_mask ) >> 24 ) // shift 24 pos
<< std::endl;
std::cout
<< std::setw(WIDTH)
<< "Green is: "
<< ( ( my_rgba_color & g_mask ) >> 16 ) // shift 16 pos
<< std::endl;
std::cout
<< std::setw(WIDTH)
<< "Blue is: "
<< ( ( my_rgba_color & b_mask ) >> 8 ) // shift 8 pos
<< std::endl;
std::cout
<< std::setw(WIDTH)
<< "Alpha is: "
<< ( ( my_rgba_color & a_mask ) >> 0 ) // shift 0 pos
<< std::endl;
}
#endif