This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Cast on bitfield ignored in gcc 2.95.2
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Cast on bitfield ignored in gcc 2.95.2
- From: Jennifer Anderson <jennifer at vmware dot com>
- Date: Mon, 17 Jan 2000 11:39:34 -0800
- cc: jennifer at vmware dot com
It looks like gcc is ignoring a cast on a bitfield in the code below.
I have an unsigned bitfield of size one byte. The bitfield is set to
0xff, cast to unsigned int and then shifted left 24 bits to give
0xff000000. I then test this value to see if it's less than 1. I
expect the test to fail, as it should be treated as an unsigned
quantity; however it is being treated as signed and the test succeeds.
For comparison, I do the same operations on an unsigned char and in
that case the resulting value is treated as unsigned, and the test
fails.
Here's the code:
#include <stdio.h>
typedef struct thestruct {
unsigned field1 : 8;
} thestruct;
main(void)
{
unsigned char c;
thestruct x;
x.field1 = 0xff;
c = x.field1;
if ((c << 24) < 1) {
printf("good: unsigned char promoted to int: c = 0x%x\n", c);
}
if ((x.field1 << 24) < 1) {
printf("good: unsigned bitfield promoted to int: field1 = 0x%x\n",
x.field1);
}
if ((((unsigned int) c) << 24) < 1) {
printf("bad: cast to unsigned int on char ignored: c = 0x%x\n", c);
}
/*
* This case has the error.
*/
if ((((unsigned int) (x.field1)) << 24) < 1) {
printf("bad: cast to unsigned int on bitfield ignored: field1 = 0x%x\n",
x.field1);
}
}
==============================
Here's what happens when I run it:
> gcc -o bitfield-cast bitfield-cast.c
> bitfield-cast
good: unsigned char promoted to int: c = 0xff
good: unsigned bitfield promoted to int: field1 = 0xff
bad: cast to unsigned int on bitfield ignored: field1 = 0xff
> gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.95.2/specs
gcc version 2.95.2 19991024 (release)
I'm running on a Pentium III box, with RedHat Linux 6.1. I installed
gcc 2.95.2 from an RPM that I grabbed from http://www.rpmfind.net/.
-- Jennifer