...

/

Left Shift and Right Shift Operators

Left Shift and Right Shift Operators

Learn the functionality of the left shift and right shift operators in C.

The left shift operator


The left shift operator moves all the bits in operand1 to the left by the number of places specified in operand2.


The left shift operator is a binary operator that operates on two operands as shown below:

Example

5 << 2 shifts all the bits in 5 two places to the left. The binary of 5 is 00000101; therefore, upon shifting all the bits two places to the left, the answer would be 00010100, which in decimal is 20.

📝 Note: When the bits are shifted to the left, blanks will be created on the right. These blanks will be filled by 0's.

Program

See the code given below!

Press + to interact
#include<stdio.h>
int main() {
// Initialize variable
unsigned char ch = 33 ;
// Display number
printf( "%d ", ch ) ;
// Display number in decimal format after left-shifting
printf( "%d ", ch << 1) ;
// Display number in hex format after left-shifting
printf( "%#x ", ch << 1) ;
}

Line 9: ...

Access this course and 1400+ top-rated courses and projects.