in reply to behavior of bitwise shift

Not sure how the this is behaving, but I get the same answers in C:

#include <stdio.h> int main(int argc, char **argv) { int a; printf( "Int size: %d\n", sizeof( a ) ); printf( "8 << 0 == %d\n", 8 << 0 ); printf( "8 << 8 == %d\n", 8 << 8 ); printf( "8 << 16 == %d\n", 8 << 16 ); printf( "8 << 24 == %d\n", 8 << 24 ); /* With this line in, gcc gives me: "test.c:12: warning: left shift count >= width of type" */ /* printf( "8 << 32 == %d\n", 8 << 32 ); */ return 0; }

Output:

Int size: 4 8 << 0 == 8 8 << 8 == 2048 8 << 16 == 524288 8 << 24 == 134217728

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Replies are listed 'Best First'.
Re^2: behavior of bitwise shift
by pg (Canon) on Oct 25, 2004 at 17:28 UTC
    "but I get the same answers in C:"

    That's for sure, as Perl's << does nothing more than simply calling c's <<. The behavior was expected. For 32 bit c, the result for 8 << 32 is undefined.