 # Right Shift Operator in C

It is a bitwise operator that we use in the C language for operating on bits. The right shift operator is binary- which means that the working of this operator would require two of the operands. We represent the right shift operator using the >> sign.

In this article, we will take a closer look at Right Shift Operator in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

## Use of the Right Shift Operator in C

The right shift operator is very similar to the left shift operator. The difference is that it is used for shifting the bits of the values that are available with us to the right. It does so by replacing the first bits with zeroes (0) instead. The excess bits that get shifted off to the right of the operator get discarded after this.

## Syntax of Right Shift Operator in C

Here is the syntax of the right shift operator in the C language:

shifted_value = old_value >> amount;

As you can see, the above statement has two values. The right one shifts the bits of the first available operand. The second operand, on the other hand, decides how many numbers of positions that the bits must be shifted to the right according to a user.

## Working of the Right Shift Operator in C

The right shift operator requires two operands for working properly in a code. Both of these operators used here must be integers. Also, the operator shifts the bits available from the first operand to the right on the basis of the number of positions that the second operand has specified. Simultaneously, it fills the empty spaces of the first bits with zeroes (0).

Note: Whenever we perform this operation on an integer a with an integer b, the result obtained after the operation is always going to be equal to the division of a with 2^b. So if a = 40 and b = 1. Thus, the result obtained from a>>b, i.e., 40>>1, will be 40 / 2^1, which will be equal to 20.

For Example,

Let us consider the number 14. Now, the binary representation of this number is 00001110 in 8 bits (for convenience, let us consider it this way).

In this case, the 14 >> 1 will shift this binary sequence by a single position to the right. Like, In this diagram, you can clearly see that whenever a number is shifted by one position to the right, the generated output is exactly the original number divided by two.

It means that if we shift 14 to the right by 1 position, the generated output would be 14 >> 1 = 7, i.e, 14 / 2¹ = 14 / 2 = 7

Also, if we shift 14 to the right by 2 positions, the generated output would be 14 >> 2 = 3, i.e, 14 / 2² = 14 / 4 = 3 (nearest integer)

Thus, in general, we can say that if we shift any given number to the right by n times (using the right shift operator in C), then the generated output is going to be number / (2n).

## Example of Right Shift Operator in C

Let us take a look at an example of how we use the right shift operator in C in a given code.

#include <stdio.h>

int main() {

int a = 10; // 1010

int b = 0;

for(b;b<2;b++)

printf(“The right shift will be %d: %d\n”, b, a>>b);

return 0;

}

The output obtained in this case would be:

The right shift will be 0: 10

The right shift will be 1: 5

## Some Important Points to Remember

1. If we try to shift the given number/ integer more than its actual size, then the behavior will be undefined.

2. The right-shift by 1 is equivalent to the division of the first term with the second term raised to the power of 2. Example, for 1 >> 3 = 1 / pow (2,3). But just like we mentioned in the first point, this would only work if the numbers used in the operands (integers) turn out to be positive.

## Practice Problems on the Right Shift Operator in C

1. What would be the result obtained by using a right shift operator on 23 >> 2?

A. 6

B. 1

C. Undefined

D. 13

Answer – A. 6

If we shift 128 to the right by 5 positions, the generated output would be 23 >> 2 = 4, i.e, 23 / 2² = 23 / 4 = 6 (closest integer).

2. What would be the result obtained by using a right shift operator on 128 >> 5?

A. 120

B. 4

C. 11

D. Undefined

Answer – B. 4

If we shift 128 to the right by 5 positions, the generated output would be 128 >> 5 = 4, i.e, 128 / 25 = 128 / 32 = 4

3. What would be the result obtained by using a right shift operator on 64 >> 3?

A. 60

B. 12

C. Undefined

D. 8

Answer – D. 8

If we shift 128 to the right by 5 positions, the generated output would be 64 >> 3 = 4, i.e, 64 / 23 = 64 / 8 = 8

## Frequently Asked Questions

### Can we use the right shift operator with the negative numbers?

No. the right shift operator mostly works with the positive integers. We must not use a negative number. When either of the operands is negative, the result obtained will be undefined.

### What is the right shift in C language?

In the right shift operator, the left operand’s value is moved right by the number of bits specified by the right operand.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,