I wouldn't overrate the uses of bitmasks. Sure, they save memory, bit in situations where memory is that important, you probably shouldn't have used Perl in the first place. Furthermore, bitmasks aren't completely portable; the number of bits in an integer isn't the same everywhere.

Also, the bitwise operators aren't playing the "there's no difference between a string and a number" game in Perl very well:

perl -wle 'print +("30" | "50") == (30 | 50) ? "Yes" : "No"' No

Forgetting that commandline arguments, or regex captures are strings, even if they only contain numbers will bite you sooner or later. And no use of strict or warnings is going to catch that for you - Perl will just think you are doing a string-wise AND or OR.

If I work with bitfields, I tend to prefer bitstrings, and get the individual bits out of them with vec. This will save memory as well, is more flexible (instead of 2 values, you can opt for 4, 8, 16, etc as well), and doesn't have the idiosyncrasies you have with the bitwise operators.

Abigail


In reply to Re: What A Wonderful World: Bitmasks! by Abigail-II
in thread What A Wonderful World: Bitmasks! by Revelation

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.