1

I have encountered <<++ and >>++ operators many time in`C++, but I don't understand what they are. What is the specific meaning and use of these operators, and how are they different from right shift and left shift operator?

Justin
  • 24,288
  • 12
  • 92
  • 142
Shivanshu
  • 784
  • 1
  • 5
  • 20

2 Answers2

1

C++ compilers ignore whitespace unless in certain situations such as string literals.

<<++ and >>++ is really just a bit-shift operatior << or >>, followed by an increment operator ++.

Consider this code:

  • a <<++ b is equivalent to
  • a<<++b because the spaces are ignored in this context, and then equivalent to
  • a << ++b (a left shifted by a pre-incremented b)
  • a << (++b) due to operator precedence. Bit shift operators have lower precedence than incrementation.
  • 1
    That's a weird way to describe it. `a <<++ b` is equivalent to `a<<++b`, but it's not because spaces are ignored. It's more that the tokenizer parses it as `a, <<, ++, b` since it ignores whitespace (it's not that you remove the spaces, it's that the spaces are ignored), which is bound like `a << ( ++ b)` due to operator precedence. – Justin Jun 03 '18 at 05:45
0

There are two separate operators in both cases: left shift (<<), right shift (>>) and increment operator (++).

You can rewrite the following:

a >>++ b

as:

a >> (++b)
joe_chip
  • 2,468
  • 1
  • 12
  • 23