Bit shifting

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:

1 of 5

Code

For these bitwise operations, we will use:

  • >> for right-shift
  • << for left-shift

Remember: 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 form
cout << "Before shifting " << bitset<8>(a);
// right-shift bits one time
int b = a >> 1;
// pritning
cout << "\n" << "After shifting bits once to the right " << bitset<8>(b);
// left-shift bits twice
b = a << 2;
// pritning
cout << "\n" << "After shifting bits twice to the left " << bitset<8>(b);
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved