What is >>> operation in C ++

In this blog post, the author suggested the following how to fix the error:

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

Does anyone know what this operator is โ†’ โ†’? Of course, it is not in the following list of operator links:

What is it and how can it solve the overflow problem?

+6
c ++ syntax
source share
6 answers

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

Check out the Java interactive tutorial here on bit shifting operations. It says:

The unsigned right shift operator "โ†’>" shifts the zero to the leftmost position, and the leftmost position after "โ†’" depends on the expansion of the sign.

+8
source share

The >>> operator is in a piece of Java code, and it is an unsigned right shift operator. It differs from the >> operator when processing symbolic values: the >> operator applies the sign extension during the shift, and >>> simply inserts zero in the bit positions that are "empty" by the shift.

Unfortunately, in C ++ there is no such thing as a save sign and an unsigned shift to the right, we only have a >> operator, whose behavior on negative values โ€‹โ€‹of the sign is determined by the implementation. To emulate a behavior similar to one of >>> , before applying the shift, you need to perform some casts to unsigned int (as shown in the code snippet immediately after the message you posted).

+3
source share

>>> not a C ++ operator. I think this is a Java operator. I'm not sure though!

EDIT:

Yes. This is a java statement. Check out the link to the article you provided. This article uses the Java language!

+2
source share

>>> - logical shift right operator in Java .

It shifts to zero on the left, and does not preserve the sign bit. The blog post author even provides an implementation in C ++:

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

... if you correctly shift unsigned numbers, saving the sign bit does not make sense (since there is no sign bit), so the compiler obviously uses logical shifts, not arithmetic ones.

The code above uses MSB (32nd bit, assuming 32-bit integers): adding low and high , which are non-negative integers and correspondingly placed in 31 bits, never overflow the full 32 bits, but they apply to MSB By moving it to the right, the 32-bit number is effectively divided by two, and the 32nd bit is cleared again, so the result is positive.

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

+2
source share

This is a java operator not related to C ++.

However, all that the author of the blog does is change the division by 2 with a correction right shift (i.e., the right shift of the value by 1 similar to division by 2 ^ 1 ).

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

+1
source share

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

+1
source share

All Articles