1: //bit_operators.cpp
2: //Illustrates the effects of some C++ bitwise operators.
4: #include <iostream>
5: using namespace std;
7: int main()
8: {
9: cout << "\nThis program demonstrates the effect of applying some of "
10: "the C++\nbitwise operators to an integer value.\n\n";
12: int i, j;
14: i = 7;
15: j = i << 2; //The "left shift" operator
16: cout << i << " left-shifted by 2 becomes " << j << endl;
17: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
19: i = 43;
20: j = i >> 3; //The "right-shift" operator
21: cout << endl << i << " right-shifted by 3 becomes " << j << endl;
22: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
24: i = 43;
25: j = i & 15; //The "bitwise and" operator
26: cout << endl << i << ", after a \"bitwise and\" with 15, becomes "
27: << j << endl;
28: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
30: i = 42;
31: j = i | 28; //The "bitwise or" operator
32: cout << endl << i << ", after a \"bitwise or\" with 28, becomes "
33: << j << endl;
34: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
36: i = 42;
37: j = i ^ 28; //The "bitwise exclusive-or" operator
38: cout << endl << i << ", after a \"bitwise exclusive-or\" with 28, "
39: << "becomes " << j << endl;
40: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
42: i = 2147463647;
43: j = ~i; //The "bitwise complement" operator
44: cout << endl << i << ", after a \"bitwise complement\", becomes "
45: << j << endl;
46: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
47: }