Bitwise operators are a concept that deals with levels of individual bits. In the early days of computers, memory was very expensive and computers did not have much of it. You must have some idea of how integers are represented in binary. For example, the decimal number 3 is represented as 11 in binary. Likewise, the decimal number 5 is represented as 101 in binary. If you ever do any programming with hardware in C++, it can be a very handy concept to understand.
In the table below are the 6-bit manipulation operators which you can useĀ
Run through the example code of bitwise operators. Go through each one to understand which bitwise operation is running for each one.
#include <iostream> using namespace std; int main() { unsigned int a = 60; // 60 = 0011 1100 unsigned int b = 13; // 13 = 0000 1101 int c = 0; c = a & b; // 12 = 0000 1100 cout << "Line 1 - Value of c is : " << c << endl ; c = a | b; // 61 = 0011 1101 cout << "Line 2 - Value of c is: " << c << endl ; c = a ^ b; // 49 = 0011 0001 cout << "Line 3 - Value of c is: " << c << endl ; c = ~a; // -61 = 1100 0011 cout << "Line 4 - Value of c is: " << c << endl ; c = a << 2; // 240 = 1111 0000 cout << "Line 5 - Value of c is: " << c << endl ; c = a >> 2; // 15 = 0000 1111 cout << "Line 6 - Value of c is: " << c << endl ; return 0; }
Share this post