Javatpoint Logo
Javatpoint Logo

Shift Operators in C

This section will discuss the Bitwise shift operators in the c programming language. Bitwise shift operator is used to shift the binary bits either in the left direction or right direction according to the program's requirement.

Shift Operators in C

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

  1. Left Shift Operator
  2. 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

In the above syntax, var_name 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 no_of_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

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

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

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

In the above syntax, var_name 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 no_of_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

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

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

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 






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA