# Shift binary numbers in C++

In this article let’s discuss how we can shift the binary numbers with the help of some example codes in C++. Shift operations are bit-wise operations because we perform these operations on binary value bits of a number.

In C++, you can shift a binary number to the left or right using the shift operators << and >>, respectively.

For example, to shift the binary number `1010`

two places to the left, you can write:

int x = 0b1010; // x is 10 in decimal int y = x << 2; // y is 40 in decimal

The result of the shift is the value of `x`

multiplied by 2 raised to the power of the number of places you shift it. In this case, `y`

is equal to `10 * 2^2`

, which is equal to 40.

To shift a binary number to the right, you can use the `>>`

operator. For example:

int x = 0b1010; // x is 10 in decimal int y = x >> 1; // y is 5 in decimal

The result of the shift is the value of x divided by 2 raised to the power of the number of places you shift it. In this case, y is equal to `10 / 2^1`

, which is equal to 5.

You can also shift a binary number by more than one place at a time. For example:

int x = 0b1010; // x is 10 in decimal int y = x << 3; // y is 80 in decimal

This shifts x three places to the left, resulting in a value of 10 `* 2^3`

, which is equal to 80.

It’s important to note that shifting a binary number can result in loss of precision if the number is too large to fit in the variable type you are using to store it. For example, if you try to shift a long long value more than 63 places to the left, the result will not be accurate because the value will not fit in the 64-bit storage space of the long long type.

In general, we have two different types of shift operators. They are :

- Left-shift operator (<<)
- Right-shift operator (>>)

## Left-shift bitwise operator :

When we perform shift operations on any number then either multiplication or division with some power of 2 will happen. To understand this clearly let us get into the topic.

Below is the syntax to perform the left shift operation where, “<<” is an operator and n, and p are operands.

**Syntax :**

n << p

Where ‘n’ is the number on which we are performing shift operations and ‘p’ number of places we shift the bits of that number.

**Example :
**

res = 9 << 3 = (00001001)<<3 = 01001000 = 72

When we perform left shift operation on 9 for 3 places, all bits are moved to the left side by 3 times. When we perform a left shift the number n is multiplied by 2 power n which means n*(2^p) will be performed. In this example, n is 9 and p is 3. Therefore,

res = 9*(2^3) = 9*8 = 72

#include <iostream> using namespace std; int main() { int n,p; n=9; p=3; int res = n<<p; cout<<n<<" << "<<p<<" = "<<res <<"\n"; return 0; }

**Output :**

9 << 3 = 72

## Right-shift bitwise operator :

Below is the syntax to perform right-shift operations on a number. Where “>>” is an operator and n,p are operands. When we perform shift operations its binary representation is considered since shift operations are performed on bits only not directly on decimal representations.

**Syntax:**

n >> p

Here, n is a number whose bits are shifted to the right side by p times. When we perform a right shift operation on a number then the number will be divided by 2 power n value. Let us see an example to understand it better.

**Example :
**

res = 9 >> 3 = (00001001) >> 3 = 00000001 = 1

Here when we perform 9>>3, the rightmost 3 bits of the binary representation of 9 will be shifted and gives us a result

res = n/(2^p) = 9/(2^3) = 9/8 = 1

Let’s look into the code

#include <iostream> using namespace std; int main() { int n,p; n=9; p=3; int res = n>>p; cout<<n<<" >> "<<p<<" = "<<res <<"\n"; return 0; }

**Output :**

9 >> 3 = 1

Hope you have understood everything we have discussed.

## Leave a Reply