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?
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
isE1
right-shiftedE2
bit positions. IfE1
has an unsigned type or ifE1
has a signed type and a non-negative value, the value of the result is the integral part of the quotient ofE1/2^E2
. IfE1
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With