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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.