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:
Date: Sun, 30 Mar 2008 17:56:38 -0400
Newsgroups: comp.lang.c

Boltar <boltar2003@xxxxxxxxxxx> 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?

That the shift count must be *less than* the number of bits in the
operand or you get undefined behavior.

-Larry Jones

Please tell me I'm adopted. -- Calvin

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