<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/72982>72982</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Inconsistent Comparison Results of Unsigned Bit Fields Across Different Compilers and Architectures
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
gyuminb
</td>
</tr>
</table>
<pre>
### **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:
```c
#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.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMV11v47oR_TX0yyCCLCWO_eAHr7O5XeAuCiTp9rGgqJHEliZVftjr_vpiSMmyHSc3gOA4FD8Oz5w5M-bOyVYjrtnDN_bwNOPBd8au22PYSV3NKlMf16wo0wOs2LBi8_c92r3EAys3aYDlTywfPjcapBZGO-k8anGEjjuoEDWYyqHdYw1Sg-8QKuz4XhoLpoFaNg1a1B6E2fVSoXVw6FDHf7mVuh3OZos86Ai5Zos8jUElPTQSVe3gIH0HGlvu5R5Bao8tWpfBa49CNlJwpY6s2MLP119bMBoq4zv4vVz8a3EPXNeweflJ36zopEfhg0UHvTV1EOiAg0UXlCfEJzj5hKMxFvgI2Rm6Jvfw55-_fsa9_9huAfdcBe4RuJu2mJ-2yOCtk-6KQculQwf_Dei8NNoBr0zwkUNhrEXhJy65EMbWxJc3ccYWnOe65raOGGjIopK8kkr6I93EBdGdgXbAhTXOQa-4b4zduew8vtdi-K730hq9Q-1v6uEOtmNEWblJvAsF81VWLrOynJc5hSNyxIrlfJnlWc6KFQ0SX6xYehv0f2ho2G9zHhzaM8aMFqQ4jvPe0PmotjhHqUFanDgE0ycqWbGUWqgQKdtzK01w8eVO_i_NVLhH5U7n32bhCT2XCmt49dg74v4FB9ncZOWtQ5DOBfqcEiMqnusvST0qXWrg4LwNwtNGKYiY3gF_lweDus4EGnWDbso_dRzDPyUi6eYiI4jrQydFR4fWuDPaecv9lNqNUcociFJhagSnZd9j1Mc5h4s8PeLEaQoFAiu3ztfSZB0rv6e3wy2fFW__ZlSNFtjjt_QKAGBkCqTbiHhnivqclcMc9vg0ff9gw0bx9tVzHxyw8on2n79f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