In this C++ tutorial, you shall learn about Left-shift Assignment operator, its syntax, and how to use this operator, with examples.

C++ Left-shift Assignment

In C++, Left-shift Assignment Operator is used to left shift the value in a variable (left operand) by a value (right operand) and assign the result back to this variable (left operand).

The syntax to left shift a value in variable x by 2 places and assign the result to x using Left-shift Assignment Operator is

x <<= 2

Example

In the following example, we take a variable x with an initial value of 5, add left shift by 2 places, and assign the result to x, using Left-shift Assignment Operator.

main.cpp

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    x <<= 2;
    cout << "x : " << x << endl;
}

Output

x : 20
Program ended with exit code: 0
ADVERTISEMENT

Conclusion

In this C++ Tutorial, we learned about Left-shift Assignment Operator in C++, with examples.