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

C++ Right-shift Assignment

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

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

</>
Copy
x >>= 2

Example

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

main.cpp

</>
Copy
#include <iostream>
using namespace std;

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

Output

x : 1
Program ended with exit code: 0

Conclusion

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