What is Bit Shifting? What is difference between Left Shifting and Right Shifting? How can we set bits of a byte in our C++ app? How can we slide bits in data to the left or to the right?

Table of Contents

## What is a “bit” in C++ software?

The bit is the most basic unit of information in computing and digital communications. In real all operators are mainly based on **Bit Operations** which are also called **Bitwise Operations**. In computer programming, a **Bitwise Operation** operates on a bit string, a bit array, or a binary numeral (considered as a bit string) at the level of its individual bits, 1s, and 0s. The Bitwise Operation is basic to the higher-level arithmetic operations, and it is a fast and simple action because it is directly supported by the processors. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.

Because of all these basics of the microarchitecture of computers, it is very important to know Bitwise Operators. C Programming language is one of the oldest programming languages and a lot of operands, operators in other programming languages got inspiration from this language. C and C++ have the same operators and most of them are the same in other programming languages. We have explained well about operators in general in this Learn How To Use Operators In C++ post before. Now let’s see Bit Shifting and Encoding – Decoding examples.

## What does bit shifting mean in a C++ app and how can I do it?

One of the Bitwise Operand is the **Bit Shifting**, the **Left Shifting** with ‘**<<**‘ operand, and the **Right Shifting** ‘**>>**‘ operand. Bit operations are the fastest operations in machine codes and in C++ because of the microarchitecture of computers as explained above. There are many encoding and decoding methods, also hash coding methods. One of the easiest and the fastest encoding method is Bit Shifting Data. Let’s see how we can do Left and Right Shifting.

### Here is how to left Shift a bit in C++

Left Shift, shifts bits of data to the left. For example, if we left shift;

this data “01110010” will be “11100100”

1 2 3 4 |
std::bitset<8> x, y; y= x<<1; // Left shifting |

### Here is how to right shift a bit in C++

Right Shift, shifts bits of data to the right. For example if we right shift;

this data “01110010” will be “00111001”

1 2 3 4 |
std::bitset<8> x, y; y= x>>1; // Right shifting |

### How to perform circular left shifting and circular right shifting of bits in C++

These shifting operators works well in lower than 127 char numbers to encode and decode. When shifting we lost the frontier bits (when shifting left we lost left bit or bits and when shifting right we lost right bit or bits). To hold all these bits in a binary data we should do **Circular Bit Shifting**,

If you we **Circular Bit Shifting**, we never loose any bits when we encode or decode our data. If we want to shift 2 bits from maximum of 8 bits we can do left and right circular bit shifting as below,

1 2 3 4 |
c =(c << 2)|(c >> (8 - 2); // Encoding with Circular Left 2 Bits Shifting c =(c >> 2)|(c << (8 - 2); // Decoding with Circular Right 2 Bits Shifting |

### How to perform circular left shifting and circular right shifting of bits in C++ with complexity

We can add complexity to number of shifting by adding (1+i%7) for example,

1 2 3 4 |
c =(c << (1+i%7))|(c >> (8 - (1+i%7)); // Encoding by Circular Left Bits Shifting with Complexity c =(c >> (1+i%7))|(c << (8 - (1+i%7)); // Decoding by Circular Right Bits Shifting with Complexity |

## Here is a full example of how to use bit shifting in your C++ app

For example we can move bits of a bitset to the left or right as in this example,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <string> #include <bitset> int main() { std::bitset<8> x, y, z; x[0] = 0; // Setting each bits of x bitset x[1] = 1; x[2] = 0; x[3] = 0; x[4] = 1; x[5] = 1; x[6] = 1; x[7] = 0; std::cout << "Original Bits : " << x << "\n"; y= x<<1; // Left shifting std::cout << "Left Shifting <<: " << y << "\n\n"; std::cout << "Original Bits : " << x << "\n"; z= x>>1; // Right shifting std::cout << "Right Shifting >>: " << z << "\n"; std::cout << "Original Bits : " << x << "\n"; y =(x << 2)|(x >> (8 - 2)); // Circular Left Shifting std::cout << "Circular Left Shifting 2<<: " << y << "\n\n"; std::cout << "Original Bits : " << x << "\n"; z =(x >> 2)|(x << (8 - 2)); // Circular Right Shifting std::cout << "Circular Right Shifting 2<<: " << z << "\n\n"; getchar(); return 0; } |

and the output will be,

1 2 3 4 5 6 7 8 9 10 11 12 13 |
Original Bits : 01110010 Left Shifting <<: 11100100 Original Bits : 01110010 Right Shifting >>: 00111001 Original Bits : 11110010 Circular Left Shifting 2<<: 11001011 Original Bits : 11110010 Circular Right Shifting 2<<: 10111100 |

Design. Code. Compile. Deploy.

Start Free Trial

Free C++Builder Community Edition