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 << 2shifts every bit ofnumbertwo positions toward the more significant bits.- The binary representation of
5is:5 = 00000101â‚‚ - After shifting left by two positions:
00000101â‚‚ << 2 = 00010100â‚‚ - The binary value
00010100â‚‚is equal to the decimal value20. - 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.