[Bug c/16039] New: Explicit casting doesn't work as expected

chanhduy dot tran at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu Jun 17 20:08:00 GMT 2004


Bug 7018 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7018) states that 
treating wide bit-fields as 'int' is acceptable.  However, explicit casting to 
work around this does not produce expected results.

-----8<-----[cut here]-----8<-----
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

struct A
{
  uint64_t base : 16;
};

int main (int argc, char *argv[])
{
  struct A a = { base: 0xFFFF };
  uint64_t x;

  x = a.base << 20;
  printf("0x%016"PRIx64"\n", x);

  x = ((uint64_t)a.base) << 20;
  printf("0x%016"PRIx64"\n", x);

  x = ((uint64_t)((uint32_t)a.base)) << 20;
  printf("0x%016"PRIx64"\n", x);

  return 0;
}
-----8<-----[cut here]-----8<-----

This produces:

  0xfffffffffff00000
  0xfffffffffff00000
  0x0000000ffff00000

If anything, I think it should print:

  0xfffffffffff00000
  0x0000000ffff00000
  0x0000000ffff00000

The only command line flag I am using is -Wall.

This is my environment:

$ uname -a
Linux kenshin 2.6.6 #6 SMP Wed May 19 18:40:37 CDT 2004 i686 unknown
ctran@kenshin$ gcc --version
gcc (GCC) 3.2.3
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc -v
Reading specs from /usr/lib/gcc-lib/i486-slackware-linux/3.2.3/specs
Configured with: ../gcc-3.2.3/configure --prefix=/usr --enable-shared --enable-
threads=posix --enable-__cxa_atexit --disable-checking --with-gnu-ld --verbose -
-target=i486-slackware-linux --host=i486-slackware-linux
Thread model: posix
gcc version 3.2.3

-- 
           Summary: Explicit casting doesn't work as expected
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: chanhduy dot tran at gmail dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16039



More information about the Gcc-bugs mailing list