Shift Operators In C

Shift operators are classified into two types based on the shifting position of the bits.

  • Left Shift Operator
  • Right Shift Operator
  • Left Shift Operator

The left shift operator is a type of Bitwise shift operator, which performs operations on the binary bits. It is a binary operator that requires two operands to shift or move the position of the bits to the left side and add zeroes to the empty space created at the right side after shifting the bits.

Syntax

Example

var_name << no_of_position

In the above syntax, varname represents the integer variable name on which the left shift (<<) operation is to be performed to shift the binary bits at the left side. And the noof_position variable represents the number of bits to be placed or shifted at the left side. In other words, the left shift operator shifts the binary bits of the first operand on the left side by the defined number of bits to the second operand.

For example, the value of the integer variable num is 22, and its binary form is 10110. Now we use the left shift operator to shift the binary bits 2, the num = num << 2 equal to the num = num (2^2). And the new value of num is 22 (2 ^ 2) = 88, which is equal to the binary form 1011000.

Example 1: Program to demonstrate the use of the Left Shift operator in C

Example

#include <stdio.h>
int main ()
{
// declare local variable
int num;
printf (" Enter a positive number: ");
scanf (" %d", &num);
// use left shift operator to shift the bits
num = (num << 2); // It shifts two bits at the left side
printf (" \n After shifting the binary bits to the left side. ");
printf (" \n The new value of the variable num = %d", num);
return 0;
}

Output

Output

Enter a positive number: 25
After shifting the binary bits to the left side.
The new value of the variable num = 100

Example 2: Program to use the Left Shift operator in the unsigned int data of the C

Example

#include <stdio.h>
int main ()
{
// declare local variable
unsigned int num = 0xff;
// use left shift operator to shift the bits
num = (num << 2);
printf (" \n After shifting the binary bits to the left side. ");
printf (" \n The new value of the unsigned variable num = %d", num);
return 0;
}

Output

Output

After shifting the binary bits to the left side.
The new value of the unsigned variable num = 1020

Example 3: Program to input the positive number from the user to perform the Left shift operator

Example

#include <stdio.h>
int main ()
{
// declare local variable
int num, bit;
printf (" Enter a positive number: ");
scanf (" %d", &num);

printf (" No. of binary bits shifted to the left side: ");
scanf (" %d", &bit);
// use left shift operator to shift the bits
num = (num << bit);
printf (" \n After shifting the bits to the left side. ");
printf (" \n The new value of the num = %d", num);
return 0;
}

Output

Output

Enter a positive number: 40
No. of binary bits shifted to the left side: 4
After shifting the bits to the left side.
The new value of the num = 640

In the above example, the binary bit of the user-defined positive number 40 is 101000. After that, we take 4 as the number to shift the binary bits on the left side. And then, the left-shifts operator shift 4 binary bits at the left side, and then space is created at the right side, which is filled or added by 4 zeroes to the right side that returns the binary value 1010000000, which is equivalent to the decimal number 640.

Right shift operator

The right shift operator is a type of bitwise shift operator used to move the bits at the right side, and it is represented as the double (>>) arrow symbol. Like the Left shift operator, the Right shift operator also requires two operands to shift the bits at the right side and then insert the zeroes at the empty space created at the left side after shifting the bits.

Syntax

Example

var_name >> no_of_position

In the above syntax, varname represents the integer variable on which the right shift (>>) operation is to be performed to shift the binary bits at the right side. And the noof_position variable represents the number of bits to be placed or shifted to the right side. In other words, the right shift operator shifts the binary bits of the first operand at the right side by defining the total number of bits to the second operand.

Example 1: Program to demonstrate the use of the Right Shift operator in C

Example

#include <stdio.h>
int main ()
{
// declare local variable
int num;
printf (" Enter a positive number: ");
scanf (" %d", &num);
// use right shift operator to shift the bits
num = (num >> 2); // It shifts two bits at the right side
printf (" \n After shifting the binary bits to the right side. ");
printf (" \n The new value of the variable num = %d", num);
return 0;
}

Output

Output

Enter a positive number: 25

After shifting the binary bits to the right side.
The new value of the variable num = 6

Example 2: Program to use the Right Shift operator in the unsigned int data of the C

Example

#include <stdio.h>
int main ()
{
// declare local variable
unsigned int num = 0xff;
// use right shift operator to shift the bits
num = (num >> 2);
printf (" \n After shifting the binary bits to the right side. ");
printf (" \n The new value of the unsigned variable num = %d", num);
return 0;
}

Output

Output

After shifting the binary bits to the right side.
The new value of the unsigned variable num = 63

Example 3: Program to input the positive number from the user to perform the Right shift operator

Example

#include <stdio.h>
int main ()
{
// declare local variable
int num, bit;
printf (" Enter a positive number: ");
scanf (" %d", &num);

printf (" No. of binary bits shifted to the right side: ");
scanf (" %d", &bit);
// use right shift operator to shift the bits
num = (num >> bit);
printf (" \n After using the right shift operator to shift the bits at the right side. ");
printf (" \n New value of the num = %d", num);
return 0;
}

Output

Output

Enter a positive number: 40
No. of binary bits shifted to the right side: 4
After using the right shift operator to shift the bits to the right. 
The new value of the num = 2

Input Required

This code uses input(). Please provide values below: