Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -->> actually do? [duplicate]

Tags:

c++

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?

like image 649
user2687781 Avatar asked Nov 30 '22 11:11

user2687781


2 Answers

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).

like image 142
Tony Delroy Avatar answered Dec 12 '22 20:12

Tony Delroy


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.

like image 31
Potatoswatter Avatar answered Dec 12 '22 21:12

Potatoswatter