What is Shift operator - C Operator

C examples for Operator:Bit Operator

Introduction

The shift operators shift the bits in the left operand by the number of positions specified by the right operand.

You could specify a shift-left operation with the following statements:

Demo Code

#include <stdio.h> 

int main(void) { 
    //w ww  .j a  v a 2 s .  c om
    int value = 12;
    int shiftcount = 3;                    // Number of positions to be shifted
    int result = value << shiftcount;      // Shift left shiftcount positions
    
    printf("%d",result);
    return 0; 
} 

Result

The variable result will contain the value 96.

The binary number in value is 0000 1100.

The bits are shifted to the left three positions, and 0s are introduced on the right, so the value of value << shiftcount, as a binary number, will be 0110 0000.

The right shift operator moves the bits to the right.

For unsigned values, the bits that are introduced on the left are filled with zeros.


Related Tutorials