bitfield signedness
Wouter Vermaelen
wouter.vermaelen@scarlet.be
Fri Apr 29 19:22:00 GMT 2011
Hi all,
This program:
#include <stdio.h>
int main() {
struct { unsigned a : 27; } s = { 1 };
printf("%d\n", s.a < -1);
printf("%d\n", ((unsigned)s.a) < -1);
}
generates as output:
0
1
This is unexpected to me, because the field 'a' is already unsigned, so
adding an extra cast to unsigned shouldn't change anything. Using the
compiler flags -f{no-}{un}signed-bitfields doesn't have any effect.
I've tried this same program with different compilers: clang gives the
same output as gcc, vs10 generates output "1 1".
Is this a bug in gcc (or vs)? Or does the C-standard not specify the
required behavior? Is there any way to change the behavior of gcc (so
make it output "1 1")?
Thanks.
Wouter
More information about the Gcc-help
mailing list