forked from vectormars/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC54 Bitwise-Operators.cpp
75 lines (62 loc) · 1.46 KB
/
C54 Bitwise-Operators.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
#include <iostream>
using namespace std;
main()
{
/*
0
1
0101 0110
1 2 6 = 1 * 10 ^ 2 + 2 * 10 ^ 1 + 6 * 10 ^ 0 = 100 + 20 + 6
1 0 1 0 = 2 ^ 3 + 2 ^ 1 = 8 + 2 = 10
1 0 1 1 0 0 = 2 ^ 2 + 2 ^ 3 + 2 ^ 5 = 4 + 8 + 32 = 44
*/
/*
Bitwise AND - &
Bitwise OR - |
Bitwise NOT - ~ (tilde)
Bitwise XOR - ^ (caret) eXclusive OR.
0 0 0
0 1 1
1 0 1
1 1 0
Bitwise left shift <<
Bitwise right shift >>
*/
cout << (10 & 2) << endl;
/*
1 0 1 0
0 0 1 0
-------
0 0 1 0
*/
cout << (10 | 2) << endl;
/*
1 0 1 0
0 0 1 0
-------
1 0 1 0
*/
cout << (10 ^ 2) << endl;
/*
1 0 1 0
0 0 1 0
-------
1 0 0 0
*/
cout << (~10) << endl;
/*
000000000000000000000000000000000000 1 0 1 0
111111111111111111111111111111111111 0 1 0 1
*/
cout << (10 << 3) << endl; //this thing means that we are multiplying 10 by 2 raised to the power of 3
/*
000000000000000000000000000000000001 0 1 0 0
20 - decimal notation
40
*/
cout << (10 >> 1) << endl; //this thing means that we are dividing 10 by 2 raised to the power of 3
/*
00000000000000000000000000000000000 0 1 0 1
5
*/
}