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



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
.



Relevant Pages

  • Re: problem with memcpy and pointers/arrays confusion - again
    ... int line, unsigned long *total_mem) ... That's a long pointer address... ... If sizeof > sizeof which is ... if you allocate for char with sizeof < sizeof, ...
    (comp.lang.c)
  • Re: sizeof a + b and strange value
    ... The distinction between compile-time and run-time operators has ... Informally, the "sizeof" unary operator ... is misleading, but is ignored by the compiler, just as the spacing in ... and 20 does change the type of the expression from unsigned int to ...
    (comp.lang.c)
  • Re: OT: Requesting C advice
    ... int and long usually only have two distinct sizes. ... Note that what you suggest works because sizeof(.) for integer ... that char be at least 32 bits. ... Cray compiler that had 64-bit chars. ...
    (Fedora)
  • Re: sizeof
    ... int main ... The inner sizeof ... sizeof is size_t which is defined as an unsigned type. ... explicitly cast the return value of sizeof to unsigned long. ...
    (comp.lang.c)
  • [Full-disclosure] Linux kernel 0day - dynamite inside, dont burn your fingers
    ... static char b; ... static volatile int *c; ... memset (e, 0, sizeof (e)); ...
    (Full-Disclosure)