Bit shifting is performed using bitwise operators to move bits to the right or to the left. Bit shifting is helpful in low-level programming. Let’s understand this concept better with the illustration below:
For these bitwise operations, we will use:
>>
for right-shift<<
for left-shiftRemember: These binary operators are in-fix.
#include <iostream>#include <bitset>using namespace std;int main() {int a = 186; // 10111010// using bitset() fucntion to print// in binary formcout << "Before shifting " << bitset<8>(a);// right-shift bits one timeint b = a >> 1;// pritningcout << "\n" << "After shifting bits once to the right " << bitset<8>(b);// left-shift bits twiceb = a << 2;// pritningcout << "\n" << "After shifting bits twice to the left " << bitset<8>(b);return 0;}
Free Resources