comp.lang.c
[Top] [All Lists]

Re: Strange bit shifting result with gcc - am I missing something obvio

Subject: Re: Strange bit shifting result with gcc - am I missing something obvious?
From: Gordon Burditt
Date: Mon, 31 Mar 2008 02:38:18 -0000
Newsgroups: comp.lang.c

>> 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!

Isn't the above error message misleading (assuming you're running
in a typical 32-bit setup)?  The number of bits shifted is *NOT*
greater than sizeof(int)*CHAR_BIT, and the result is still undefined.

s/greater than/greater than or equal to/


<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy