Boltar wrote:
On 32 bit linux with gcc 4.2 I get unexpected results with this code:
main()
{
int bits = 32;
printf("%d\n",(int)1 << (int)32);
printf("%d\n",(int)1 << bits);
}
The first printf gives a result of 0, the second gives 1. I checked
with sizeof() and ints are definately 32 bits in size.
I'm sure I'm missing something obvious but can someone tell me what?
Thanks
B2003
When I compile tyour code with lcc-win I get:
Warning tshift.c: 2 no type specified. Defaulting to int
The prototype for main is:
int main(void)
Warning tshift.c: 5 missing prototype for printf
You did not include <stdio.h>
Warning tshift.c: 5 shift by 32 is undefined
The number of bits shifted is greater than sizeof(int)*CHAR_BIT
This is undefined!
The result of running the program is:
16777216
1
If I turn optimizations ON I get:
16777216
1024
The result of shifting more than sizeof(int)*CHAR_BIT positions
is NOT defined by the language. It is an illegal expression.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
|