GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Shift operators in C

Before we discuss the shift operators, it is important for us to know how integer numbers are represented in computer. The data is stored in computers in binary language. The shift operators are bitwise operators, and they are applied on the bit pattern. 

Please refer this on how we convert from one base system to another before going into the shift operators. 

There are two types of shift operators

  1. Left shift (<<)
  2. Right shift (>>)
Note: Shift operators can only be applied on the integers.

You may refer to the video posted at the end of this page.

Left shift operator (<<)

The left shift operator (<<) shifts the bit pattern of a number, towards the left by specified number of bits.

It is written as, y = x<<k, means shift the bit pattern of x by k bits on the LHS and store the answer in y.

Let us understand this with the help of an example.

y = 13 << 1, read as shift the bit patten of 13 towards left by 1 bit.

We convert 13 in binary which is 1101. We write 1101 in 32 bits and shift the bit pattern towards left.

Left shift

We see that the bit pattern of 13, is shifted 1 bit towards left. The MSB (31st bit) is lost. 

30th bit goes to 31st bit position.

29th bit goes to 30th bit position.

28th bit goes to 29th bit position. and so on.

The 0th bit will go to 1st bit position and the 0th bit is added with 0. (Since it is empty now)

Converting the number back to decimal we get 26.

Therefore, 13<<1 = 26

Similarly, if we do 13<<2, we will get 52. (I hope that you will try it 🙂

we observe,

13<<1 = 26 =13 * 21  

13<<2 = 52 =13 * 22

Therefore, in general

y = x<<k

y =x * 2k  

Right shift operator (>>)

The right shift operator (>>) shifts the bit pattern of a number, towards the right by specified number of bits.

It is written as, y = x>>kmeans shift the bit pattern of x by k bits on the RHS and store the answer in y.

Let us understand this with the help of an example.

y = 13 >> 1, read as shift the bit patten of 13 towards right by 1 bit.

We convert 13 in binary which is 1101. We write 1101 in 32 bits and shift the bit pattern towards right.

Right shift

We see that the bit pattern of 13, is shifted 1 bit towards right. The LSB (0th bit) is lost. 

31st bit goes to 30th bit position.

30th bit goes to 29th bit position.

29th bit goes to 28th bit position and so on.

The 1st bit will go to 0th bit position and the 0th bit is lost. 

Since the MSB (31st bit) is empty we add 0.

Converting the number back to decimal we get 6.

Therefore, 13>>1 = 26

we observe,

13>>1 = 6 =13 / 21  

13>>2 = 3 =13 / 22

Therefore, in general

y = x>>k

y =x / 2k  

int / int = int. 

Note:

  1. The left shift and right shift operators should not be shifted by negative number of bits. The result is undefined.
    1. y = x<<k
    2. y = x>>k

In both of the above cases the value of k>=0, if k<0, the result is undefined. 

Ex:

The following results are undefined.

2<<-2 and 2>>-1

2. If the number is shifted more than the number of bits, since shift can only be applied on integer (32 bits), if we shift a number for more than 32 bits, say 1<<34, the result is undefined.

Few Examples

  1. 2<<3 = 8  (2 * 2³)
  2. 1<<k = 2k 
  3. 1>>k = 1/2k 
  4. 3>>2 = 0 (3/2²)
  5. -1<<2 = -4 (-1 * 2²)
Scroll to Top