This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
gcc and 64 bit code
- From: Adam Sulmicki <adam at cfar dot umd dot edu>
- To: gcc at gcc dot gnu dot org
- Date: Fri, 9 Jul 2004 19:23:35 -0400 (EDT)
- Subject: gcc and 64 bit code
Hello,
I have this bizzare problem.
the bit shifting seems work fine on 32 bit data but not
for 64 bit data:
sample program is:
--------------------------------------------------------------
main(){
printf("Int : %i\n",sizeof(unsigned int));
printf("Long: %i\n",sizeof(unsigned long));
printf("\n");
unsigned int r1 = 1 << (sizeof(unsigned int)*8 - 1);
printf("count:%li witdth:%li result:%x\n",
sizeof(unsigned int)*8-1,sizeof(unsigned int)*8,r1);
unsigned long r2 = 1 << (sizeof(unsigned long)*8 - 1);
printf("count:%li witdth:%li result:%x\n",
sizeof(unsigned long)*8-1,sizeof(unsigned long)*8,r2);
printf("\n");
}
--------------------------------------------------------------
it compiles as follows:
--------------------------------------------------------------
# gcc test2.c
test2.c: In function `main':
test2.c:14: warning: left shift count >= width of type
--------------------------------------------------------------
and the result of running is:
--------------------------------------------------------------
# ./a.out
Int : 4
Long: 8
count:31 witdth:32 result:80000000
count:63 witdth:64 result:0
--------------------------------------------------------------
the system is as follows:
--------------------------------------------------------------
# gcc -dumpversion
3.3.3
# gcc -dumpmachine
x86_64-pc-linux-gnu
# uname -a
Linux k 2.6.7 #3 SMP Fri Jun 18 08:59:04 MST 2004 x86_64 5 GNU/Linux
--------------------------------------------------------------
The bit shifting on 32 bit data seems to work fine but it fails on 64 bit
data. How it is so? Is that because gcc uses 64 bit for all internal
calculations? Even if so, still shouldn't it warn for the 32 bit shifting
as well, not just for the 64 bit case only? It seems to me sort of
inconsistent.
Adam