## Shift Operators in CThis 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 are classified into two types based on the shifting position of the bits. - Left Shift Operator
- Right Shift Operator
## Left Shift OperatorThe 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.
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.
Enter a positive number: 25 After shifting the binary bits to the left side. The new value of the variable num = 100
After shifting the binary bits to the left side. The new value of the unsigned variable num = 1020
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 operatorThe 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.
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.
Enter a positive number: 25 After shifting the binary bits to the right side. The new value of the variable num = 6
After shifting the binary bits to the right side. The new value of the unsigned variable num = 63
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 |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]