Can someone explain to me what a bitwise shift is supposed to return and why?
I'm not sure if this question has been answered already by the other fine posts, but this helped me understand what was going on:
for (0 .. 16) { my $shifted = 255 << $_; my $bitstring = unpack "b*", pack "i", $shifted; printf "%2d %8d %s\n", $_, $shifted, $bitstring; }
The output looks like this:
0 255 11111111000000000000000000000000 1 510 01111111100000000000000000000000 2 1020 00111111110000000000000000000000 3 2040 00011111111000000000000000000000 4 4080 00001111111100000000000000000000 5 8160 00000111111110000000000000000000 6 16320 00000011111111000000000000000000 7 32640 00000001111111100000000000000000 8 65280 00000000111111110000000000000000 9 130560 00000000011111111000000000000000 10 261120 00000000001111111100000000000000 11 522240 00000000000111111110000000000000 12 1044480 00000000000011111111000000000000 13 2088960 00000000000001111111100000000000 14 4177920 00000000000000111111110000000000 15 8355840 00000000000000011111111000000000 16 16711680 00000000000000001111111100000000
This clearly shows what's going on. The bits of the original number are being shifted a certain number of times, hence it's called a "bitwise shift operator."
In reply to Re: behavior of bitwise shift
by revdiablo
in thread behavior of bitwise shift
by ngiordano
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |