• Home
  • 3.18 Left Shift Operator (<<)

3.18 Left Shift Operator (<<)

View Categories

3.18 Left Shift Operator (<<)

1 min read

Table of Contents

The left shift operator (<<) shifts the bits of the left operand to the left by the number of positions specified by the right operand. Each left shift moves the bits toward the more significant positions, and zeros are inserted into the vacated least significant bit positions.

For unsigned integers, shifting left by one position is generally equivalent to multiplying the value by 2, provided that no overflow occurs. Left shift operations are widely used in bit manipulation, embedded systems, and low-level programming.

Source Code #

#include <iostream>

int main()
{
    int number = 5;

    int result = number << 2;

    std::cout << "Original Value: " << number << '\n';
    std::cout << "Shifted Value : " << result << '\n';

    return 0;
}

Output #

Original Value: 5
Shifted Value : 20

Explanation #

  • The left shift operator (<<) shifts the bits of the left operand to the left by the specified number of positions.
  • number << 2 shifts every bit of number two positions toward the more significant bits.
  • The binary representation of 5 is:5 = 00000101â‚‚
  • After shifting left by two positions:00000101â‚‚ << 2 = 00010100â‚‚
  • The binary value 00010100â‚‚ is equal to the decimal value 20.
  • Vacated bit positions on the right are filled with zeros.
  • For unsigned integers, shifting left by one position generally multiplies the value by 2, provided no overflow occurs.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

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