Hmm...you're absolutely right. However, let's take a look at what it is printing out.

With your unpack, you are specifying b which means that the left-most bit is least significant. So what does 10001100 become? 49. What's 49? It is the ascii code for 1. If you change your unpack to "b*", @charBits contains 24 elements and looks like: 100011000100110001101100. Taken in 8 bit chunks, it turns out to be 49, 50, 54 (the ascii codes for 1, 2 and 6).

Please remember that unpack thinks whatever you give it is a string. To fix your problem, all you need to do is first pack your 126 as an integer like so: my @charBits = split(//, unpack('b8', pack("S",$temp)));. Now the code should works as expected (it should print 01111110). Please check out perlpacktut for more information.

Hope this helps.

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1


In reply to Re: using vec() and unpack() by antirice
in thread using vec() and unpack() by Gerard

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.