Home > Backend Development > C++ > body text

Why does the right shift operator (`>>`) in C produce unexpected results when shifting by 32 bits?

DDD
Release: 2024-10-25 10:17:02
Original
376 people have browsed it

Why does the right shift operator (`>>`) in C   produce unexpected results when shifting by 32 bits?
>`) in C produce unexpected results when shifting by 32 bits? " />

Unexpected Behavior of Right Shift Operator (1 >> 32)

In the realm of programming, the right shift operator (>>) is commonly used to perform bitwise operations, particularly to divide an integer by a power of two. However, peculiar behavior can arise when shifting by larger values, as demonstrated by the following C code:

<code class="cpp">int foo(int a, int b) {
   return a >> b;
}

int bar(uint64_t a, int b) {
   return a >> b;
}

int main() {
    std::cout << "foo(1, 32): " << foo(1, 32) << std::endl;
    std::cout << "bar(1, 32): " << bar(1, 32) << std::endl;
    std::cout << "1 >> 32: " << (1 >> 32) << std::endl; //warning here
    std::cout << "(int)1 >> (int)32: " << ((int)1 >> (int)32) << std::endl; //warning here
}
Copy after login

Surprisingly, the output of this program reveals unexpected results:

foo(1, 32): 1 // Should be 0
bar(1, 32): 0
1 >> 32: 0
(int)1 >> (int)32: 0</code>
Copy after login

The rationale behind these results lies in the internal workings of the CPU and the compiler.

Behavior of foo() Function

In the foo() function, the shift operation is performed without a cast, leading the CPU to perform a logical right shift. On many architectures, the logical right shift is implemented as a >> (b % 32), effectively ignoring the upper bits of b. Therefore, foo(1, 32) results in 1 >> (32 % 32), which evaluates to 1 >> 0, yielding 1.

Why Does Casting to 64-bit Integer Matter?

In the bar() function, a 64-bit unsigned integer is provided, ensuring that the result is guaranteed to be 0 because b (32) is less than the number of bits in the operand (64). However, when b is changed to 64, the result becomes unpredictable and may still yield 1.

Compiler Optimization

In the case of 1 >> 32 and (int)1 >> (int)32, the compiler optimizes these constant expressions at compile-time. The standard specifies undefined behavior for right shifts where the count is negative or greater than or equal to the length of the operand. Since 32 exceeds the length of the operand, the compiler cannot determine the result and outputs 0 as a safe fallback.

CPU-Specific Behavior

The implementation of right shift operations can vary across different CPUs. On x86/x86-64 architectures, the logical right shift is effectively a >> (b % 32 or 64), depending on the mode. However, on ARM processors, the right shift operation guarantees zero for shifts greater than or equal to 32.

Conclusion

When working with right shift operators, it is essential to consider potential undefined behaviors, especially when the shift count exceeds the operand's length. Casting to wider integer types, such as 64-bit integers, can ensure consistent results across different CPUs and compilers.

The above is the detailed content of Why does the right shift operator (`>>`) in C produce unexpected results when shifting by 32 bits?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!