This C++ program illustrates the bitwise operators. The bitwise operators are like logic gates operators which work on individual bits of binary representations of the data.
Here is the source code of the C++ program which illustrates the bitwise operators. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Illustrate Bitwise Operators
*/
#include <iostream>
using namespace std;
int main() {
int a = 7; // a = 111
int b = 5; // b = 101
cout << "Bitwise Operators\n";
cout << "a & b = " << (a&b) << "\n";
cout << "a | b = " << (a|b) << "\n";
cout << "a ^ b = " << (a^b) << "\n";
cout << "~a = " << (~a) << "\n";
cout << "~b = " << (~b) << "\n";
cout << "a >> b = " << (a>>b) << "\n";
cout << "a << b = " << (a<<b) << "\n";
}
advertisement
$ gcc test.cpp $ a.out Bitwise Operators a & b = 5 a | b = 7 a ^ b = 2 ~a = -8 ~b = -6 a >> b = 0 a << b = 224
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice Programming MCQs
- Apply for Information Technology Internship
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Buy C++ Books