Inconsistent Comparison Results of Unsigned Bit Fields Across Different Compilers and Architectures
Overview:
An inconsistency has been observed in the behavior of different compilers when comparing unsigned bit fields with negative integers. Specifically, MSVC on both x86_64 and ARM64 architectures produces a result of 0 for a comparison that LLVM and GCC evaluate as 1. This inconsistency raises questions about the correct behavior according to the C standard and the reliability of such comparisons across platforms.
Environment:
- Compilers: MSVC cl 19.38.33130, LLVM (18.0.0), GCC (trunk)
- Architectures: ARM64, x86_64
- Tested in: All compilation options (including various optimization levels)
Detailed Steps to Reproduce:
The issue is observed when an unsigned bit field within a struct is compared with a negative integer. This comparison behaves differently across compilers and architectures, which is demonstrated in the following code snippet:
#include <stdio.h>
struct FlagHolder {
unsigned isActive : 1;
};
struct FlagHolder flagStatus = {1};
int main ()
{
// The comparison should be consistent across compilers and architectures
printf("(flagStatus.isActive >= -1) result: %d\n", (flagStatus.isActive >= -1));
return 0;
}
Expected Result:
It is unclear what the expected result should be, as per the C standard. The comparison (flagStatus.isActive >= -1) yields different results across compilers and architectures, leading to ambiguity about the correct behavior.
Actual Result:
- ARM64 (MSVC) & x86_64 (MSVC): The output is
0(false) under all tested options. - LLVM & GCC (Various Architectures): The output is
1(true) under all tested options.
Additional Information:
- This issue highlights a significant discrepancy in how unsigned bit field comparisons with negative integers are handled by different compilers.
- The inconsistency is particularly concerning for cross-platform development, where such comparisons might lead to different behaviors on different architectures.
Suggested Actions:
Given the inconsistency observed, it is crucial to clarify the expected behavior according to the C standard. Developers are left unsure about which compiler behavior is correct. It would be beneficial for compiler developers and the C standard committee to provide guidance on this issue.
Request for Clarification and Resolution:
We request that compiler developers (MSVC, LLVM, and GCC) investigate this issue and provide clarification on the expected behavior. Additionally, a resolution to align the behavior across compilers would significantly enhance the reliability and predictability of C code, especially in cross-platform scenarios.
C standard says "If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int."
C++ says "A converted bit-field of integral type can be converted to a prvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field."
As far as I can tell, clang is behaving correctly.