What is >>> operation in C++

2020-03-14 02:00发布

In this blog post the author has suggested the following as the bug fix:

 int mid = (low + high) >>> 1;

Does anyone know what is this >>> operator? Certainly its not there on the following operator reference list:

What is it and how does that solve the overflow problem?

标签: c++ syntax
6条回答
一纸荒年 Trace。
2楼-- · 2020-03-14 02:35

The Java expression x >>> y is more or less equivalent to the C++ expression unsigned(x) >> y.

查看更多
家丑人穷心不美
3楼-- · 2020-03-14 02:37

>>> is not C++ operator. I think it's an operator in Java language. I'm not sure though!

EDIT:

Yes. That is java operator. Check out the link to the article you provided. The article is using Java language!

查看更多
乱世女痞
4楼-- · 2020-03-14 02:37

>>> is the logical right shift operator in Java.

It shifts in a zero on the left rather than preserving the sign bit. The author of the blog post even provides a C++ implementation:

mid = ((unsigned int)low + (unsigned int)high)) >> 1;

... if you right-shift unsigned numbers, preserving the sign bit doesn't make any sense (since there is no sign bit) so the compiler obviously uses logical shifts rather than arithmetic ones.

The above code exploits the MSB (32rd bit assuming 32 bit integers): adding low and high which are both nonnegative integers and fit thus into 31 bits never overflows the full 32 bits, but it extends to the MSB. By shifting it to the right, the 32 bit number is effectively divided by two and the 32rd bit is cleared again, so the result is positive.

The truth is that the >>> operator in Java is just a workaround for the fact that the language does not provide unsigned data types.

查看更多
我只想做你的唯一
5楼-- · 2020-03-14 02:37

It is a java operator, not related to C++.

However all the blog author does is change the division by 2 with a bit-wise right shift (i.e. right shifting the value by 1 is similar to dividing by 2 ^ 1).

Same functionality, different machine code output (bit shifting operations are almost always faster than multiplication/division on most architectures).

查看更多
再贱就再见
6楼-- · 2020-03-14 02:50

>>> is not a part of C++. The blog contains code in Java.

Check out Java online tutorial here on Bitwise shift operators. It says

The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

查看更多
我只想做你的唯一
7楼-- · 2020-03-14 03:00

The >>> operator is in a Java code snippet, and it is the unsigned right shift operator. It differs from the >> operator in its treatment of signed values: the >> operator applies sign extension during the shift, while the >>> operator just inserts a zero in the bit positions "emptied" by the shift.

Sadly, in C++ there's no such thing as sign-preserving and unsigned right shift, we have only the >> operator, whose behavior on negative signed values is implementation-defined. To emulate a behavior like the one of >>> you have to perform some casts to unsigned int before applying the shift (as shown in the code snippet immediately following the one you posted).

查看更多
登录 后发表回答