Right Shift Operator's Intriguing Behavior
The right-shift operator (>>) exhibits peculiar behavior when dealing with large right shift values. Consider the following program:
#include
#include
int foo(int a, int b)
{
return a >> b;
}
int bar(uint64_t a, int b)
{
return a >> b;
}
int main()
{
std::cout > 32: " > 32) > (int)32: " > (int)32) The expected output for foo(1, 32) would be 0, but surprisingly, it returns 1. This can be attributed to the following:
Logical Shift vs. Arithmetic Shift
On x86/x86-64 architectures, the right-shift operator is actually performing a logical right shift, which means it fills the vacated bits with 0, regardless of the sign of the left operand. The behavior is similar to using a >>> b.
Compiler Optimization
In the case of foo(1, 32), the value 32 is being cast to int, which is effectively truncated to 32 bits. Since the maximum value an int can hold is 231-1, the right shift is essentially a >>> (32 % 32), which evaluates to 0.
Undefined Behavior
The relevant C standard states that "the behavior is undefined" for right shifts with a count greater than or equal to the width of the promoted left operand. In this case, both 1 >> 32 and (int)1 >> (int)32 have a count greater than 32, leading to unpredictable results.
Difference with bar(1, 32)
The function bar takes a 64-bit unsigned integer, which is guaranteed to have a width greater than 32. Therefore, the right shift in bar is not affected by undefined behavior.
Conclusion
The behavior of the right-shift operator becomes ambiguous when dealing with large shift values. On x86/x86-64 architectures, a logical right shift is performed, while on ARM, a different implementation may be used. Due to undefined behavior, the result of right shifts with a count greater than or equal to the operand's width should be avoided in portable code.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3