4

In the question What is the "-->" operator in C++? it asks what --> does and gives a link to a comp.lang.c++.moderated thread. scrolling down the thread a bit further found me this:

> There is no such operator in C++.

> It's just a combination of two operators: postfix decrement "--" and > greater ">".

> That's why this example works.

> Try ( x --> 20 ) and you'll get no output in this case;)

Of course there is. It is described together with "runs to" operator:

#include <stdio.h>
int main()
{
   int x = 10;
   while( x -->> 0 ) // x runs to 0
     printf("%d ", x);
}

What does the "runs to" operator actually do?

Community
  • 1
  • 1
user2687781
  • 325
  • 2
  • 11
  • 2
    The answer is in your question. – hobbs May 26 '14 at 01:38
  • 2
    You have a different one in your title: `-- >>`. Funnily enough, it has almost the same behaviour. – chris May 26 '14 at 01:39
  • @chris whoops, html codes. – user2687781 May 26 '14 at 01:40
  • Your HTML entities also translate to `-->>`. Which is it you're asking about? – Potatoswatter May 26 '14 at 01:40
  • `x-->>0` is same as `x--` because `>>0` is basically nop (unless operator >> is overloaded) – Bryan Chen May 26 '14 at 01:41
  • @Potatoswatter I'm asking about -->>. Copy/paste from thread error. – user2687781 May 26 '14 at 01:41
  • @BryanChen but then how does it only go to 0? – user2687781 May 26 '14 at 01:42
  • @user2687781 Because `0 >> 0` results in `0`, and `while(0)` quits the loop. Is this really that difficult to figure out? – Praetorian May 26 '14 at 01:43
  • 8
    @user2687781 You're aware that **`-->` is not a real operator** right? They are being *sarcastic* in that thread! – Jonathon Reinhart May 26 '14 at 01:44
  • 1
    @JonathonReinhart It's *almost* a duplicate of the `-->` question, but I don't know if it's enough to be marked as a dupe. A better close reason would be *off-topic because OP refuses to think for himself* – Praetorian May 26 '14 at 01:46
  • @Praetorian Agreed, I also thought it was a typo, until I went and read the original thread... sigh. Agreed. – Jonathon Reinhart May 26 '14 at 01:47
  • +1 I like questions where I learn something new. I'm familiar with the old "runs to" `-->` operator (it was posted by Andrew Koenig in comp.lang.c++ very long ago, before Stack Overflow), but `-->>` was new to me. One difference is that it only works (intuitively) for 0 as second operand. I think this is truly in line with URLs as statements (e.g. `http://www.google.com;` as a C++ statement), and such. – Cheers and hth. - Alf May 26 '14 at 02:06

3 Answers3

12

while( x -->> 0 ) // x runs to 0

This is actually a hybrid of the -- (post-decrement) and >> (bitshift right) operators, better formatted as:

while (x-- >> 0) ...

For this specific usage, with 0 on the right hand side, x is decremented with each loop iteration due to the postfix --, and the prior (pre-decrement) value is shifted right 0 bits by >> 0 which does nothing at all when x is non-negative, so the statement could be simplified to:

while (x--) ...

When x is 1 that's non-zero so found true for the purposes of the while test, then post-decrement reduces it to 0 and the loop executes for the last time (with x being 0 during that iteration); the next time while (x--) is checked with x already 0, the while loop terminates, with x left wrapping to the highest representable value for the unsigned type.

More generally, if you try to use >> on a negative value (e.g. x starts at 0 or a negative value great than INT_MIN, so x-- yields a negative value) the result is implementation defined, which means you have to consult your compiler documentation. You could use your compiler documentation to reason about how it would behave in the loop....

Relevant part of the Standard: 5.8/3:

The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2^E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

BTW /- for Visual Studio, per http://msdn.microsoft.com/en-us/library/336xbhcz.aspx, the implementation defined behaviour is "No shift operation is performed if additive-expression is 0.". I can't find anything in the GCC manual about this (would have expected it here).

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • Since this became the accepted answer, it's probably worth pointing out that `-->>` is not a "runs to" operator - it is two operators, `--` and `>>` - the OP may be confused on this point as the answer doesn't explicitly state this. Ordinarily I'd say this should be obvious, but the existence of this question implies that such things are not obvious to the OP. – JBentley May 26 '14 at 02:14
  • @TonyD "Signed `>>` acts on negative numbers by sign extension." is sufficient to resolve the implemetation-defined behaviour for negative numbers right-shifted 0 positions. It's a bit telegraphic, but I don't see the need to special case shifting by 0 if shifting a negative number is defined, since the no-opness of shifting 0 bits is implicit in the definition of `>>` ("E1 right-shifted E2 bit positions") – rici May 26 '14 at 02:29
9
while( x -->> 0 ) // x runs to 0

No, the "goes to operator" is --> with only one > sign. It decreases x by one and then compares the result to zero.

The -- >> 0 "runs to operator" decreases x and then bitshifts the result rightward by zero. Bitshifting by zero does nothing for nonnegative x, otherwise it's implementation-defined (usually does nothing, but could be random). Zero bitshifted by zero is zero, which is interpreted as false, at which point the loop will terminate.

So it "works" but it's a terrible way of expressing a loop.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
0

-- decrements but returns the value of the variable before it was decremented, >> shifts to the right by the right operand, which is 0 (a.k.a. a no-op), then it implicitly compares the result against 0.

BonzaiThePenguin
  • 1,413
  • 13
  • 19