Why does increment operator modify the original value and bit operators don’t?

x << 1 is analogous to operations like x * 2. If you don’t store the result anywhere, it is just discarded and the line may just get omitted entirely by an optimizing compiler.

If you want to store the result of an operation like that back into x, you have options like:

x = x * 2;
x *= 2;

The << operator is the same:

x = x << 1;
x <<= 1;

Leave a Comment