Your original question deals with bitwise operators as a method of examining the bits behind a number. ...then we started talking about unpack() instead, which works, but may not satisfy the requirements of what you're trying to learn.

I implemented a solution just now using the & operator (which is a bit-wise operator), with $number & $bit_value, iterating over the values of individual bits. I don't want to ruin your homework learning experience, so I guess I'll just try to describe the method:

Start by setting $bit_value to 1. This represents the value of the least significant bit. Then take $number & $bit_value. If you get a non-zero value, that bit is 'set'. Keep track of it. Then repeat the process for $bit_value being equal to 2, then to 4, then to 8, 16, 32, and so on. Each time keep track of whether the specific bit is set.

This will give you a list of set or unset bits, in order of least significant to most significant. Once you've worked through it yourself with those hints, try posting some code, and I'll show you a comparison with how I did it.


Dave


In reply to Re: Bitwise operators by davido
in thread Bitwise operators by thevoid

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.