What is >>> operation in C++

前端 未结 6 1095
半阙折子戏
半阙折子戏 2021-01-12 06:48

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

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

Does anyone know what is t

6条回答
  •  忘掉有多难
    2021-01-12 07:13

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

提交回复
热议问题