Types of Shift Operators in C

Luqman Khan Feb 15, 2024
  1. Shift Operators in C
  2. Types of Shift Operators in C
  3. Conclusion
Types of Shift Operators in C

In this article, we will understand the shift operators, which are the types of bitwise operators done on the bits of the values of variables. There are two types of shift operators: the left shift and the right shift operator.

Let’s have a look at these operators.

Shift Operators in C

The left shift operator is characterized by the << symbol and the right shift operator by the >> symbol. These operators are binary and require two operands on both sides of operators, and both must be integers.

The syntax of the right shift arithmetic operator is given below:

n >> m ;

We are shifting the value in n to the right by m bits. Here, n and m are integers.

Types of Shift Operators in C

There is only one right shift operator >> in C. C compilers select which right shift is used depending on what type of integer is being shifted.

The arithmetic shift is used when signed integers are shifted, and unsigned integers are shifted using the logical shift.

In both cases, the logical shift takes place for the unsigned left or right shift, but we cannot use the logical shift in the case of a signed integer value. The logical shift can change the sign, so we can use the arithmetic shift to remove this problem.

So in the right arithmetic shift, the MSB (sign bit) is copied and shifted right. The effect is to divide by two.

The syntax for signed numbers:

n1=-n2;

n1>>m1;

In the case of a signed integer number, store the first bit in an empty slot and move the copy to the next during shifting.

Arithmetic and logical left shifts are the same. Only the right shifts differ for negative values.

Thus programs that do right shifts on signed values are inherently non-portable.

When shifting a value to the right by n bits, it divides the value by 2 raised to n, and shifting the value to the left by n bits multiplies the value by 2 raised to the power of n.

Example code:

#include <stdio.h>

int main(void) {
  int dd = -15;
  int ee = 15;
  printf("value of  dd right(sign bit) shift by 1 bits, is %d\n", dd >> 1);
  printf("value of  dd right shift(sign bit) by 2 bits, is %d\n", dd >> 2);
  printf("value of  dd right shift(sign  bit) by 3 bits, is %d\n", dd >> 3);
  printf("value of  dd right shift by 4 bits, is %d\n", dd >> 4);
  printf("value of  ee right shift by 1 bits, is %d\n", ee >> 1);
  printf("value of  ee right shift by 2 bits, is %d\n", ee >> 2);
  printf("value of  ee right shift by 3 bits, is %d\n", ee >> 3);
  return 0;
}

In the above example code in C, we will use the #include <stdio.h> as a library header file which has the information about including the input or output related functions such as the printf() and scanf() functions in our program.

The main function is the starting point of the program execution. The operating system initiates the program execution by invoking the main function.

The following two variables, dd and ee, are initialized by two different sign values, positive and negative. Then the shifting process takes place in the printf() function simply by 1, 2, and 3 numbers, respectively, and the same with the sign value ee.

When arithmetic right shift is performed on a negative number, the sign bit is shifted and copied to the empty slots created during shifting, as in the above example. Both values are shifted to the right, signed and unsigned numbers, for the given times.

In the unsigned number, the shifting is logical. It is represented in the ee variable containing a value 15, which is shifted by 1 bit, 2 bits, and 3 bits to the right.

It shows that dividing the value by 2 raised the power of a number (bit), e.g., shifting by 1 bit is 15/2^1, which equals 7.

While in signed value shifting, the first-bit value copies itself for shifting and stores it in an empty slot during shifting.

Output:

Right Shift - Output

The right shift for unsigned and signed values is straightforward; the vacant bits are filled with zeros. For negative values, the result of right shifting is implementation-defined.

We will use an even number in the below example to see the results and understand the working of the right shift operator in even and odd signed numbers. Let’s see the code given below.

#include <stdio.h>

int main(void) {
  int dd = -16;
  int ee = 16;
  printf("value of  dd right(sign bit) shift by 1 bits, is %d\n", dd >> 1);
  printf("value of  dd right shift(sign bit) by 2 bits, is %d\n", dd >> 2);
  printf("value of  dd right shift(sign  bit) by 3 bits, is %d\n", dd >> 3);
  printf("value of  dd right shift by 4 bits, is %d\n", dd >> 4);
  printf("value of  ee right shift by 1 bits, is %d\n", ee >> 1);
  printf("value of  ee right shift by 2 bits, is %d\n", ee >> 2);
  printf("value of  ee right shift by 3 bits, is %d\n", ee >> 3);
  return 0;
}

Output:

Right Shift - Output 2

Conclusion

It is concluded that logical shift (sign bit) causes a number sign change problem in the right shift, which can be controlled in the arithmetic right shift operator. C and C++ have only arithmetic shifting, with some areas left undefined and implementation-defined.

Related Article - C Operator