• Home
  • 3.19 Left Shift

3.19 Left Shift

View Categories

3.19 Left Shift

< 1 min read

Table of Contents

The left shift operator << shifts the bits of an integer to the left by the specified number of positions. Bits shifted beyond the width of the type are discarded, and zero bits are introduced from the right.

#include <stdio.h>

int main()
{
    unsigned int number = 5;

    printf("number << 1 = %u\n", number << 1);
    printf("number << 2 = %u\n", number << 2);

    return 0;
}

Example Output #

number << 1 = 10
number << 2 = 20

Explanation #

The value 5 in binary is:

00000101

Shifting it left by one position:

00000101 << 1
00001010

The resulting value is 10.

Shifting it left by two positions:

00000101 << 2
00010100

The resulting value is 20.

For an unsigned integer, shifting a value left by n positions corresponds to multiplying by 2^n when the resulting value is representable in the type. The exact result is determined by the bit representation and the width of the integer type.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *