Hi rir,

For my own tastes, I would use integers and masks, in the "C" style.  Of course, my background is in "C", so you might call me biased.

For example, here's a simple example of clear_bit and set_bit using a mask and a bit index:

#!/usr/bin/perl -w # Strict use strict; use warnings; # Test code my $mask = 0b11001011; clear_bit($mask, 1); clear_bit($mask, 2); clear_bit($mask, 3); clear_bit($mask, 4); $mask = 0b11001000; set_bit($mask, 1); set_bit($mask, 2); set_bit($mask, 3); set_bit($mask, 4); # Subroutines sub clear_bit { my ($mask, $bit_index) = @_; printf "TFD> mask 0b%08lb\n", $mask; $mask &= ~(1 << ($bit_index - 1)); printf "TFD> mask 0b%08lb\n\n", $mask; } sub set_bit { my ($mask, $bit_index) = @_; printf "TFD> mask 0b%08lb\n", $mask; $mask |= (1 << ($bit_index - 1)); printf "TFD> mask 0b%08lb\n\n", $mask; }

The lines containing "TFD" are temporary debugging statements which can be removed once you've verified that the code is doing what you want.

Of course, if you want to use names in place of the bit_index, you could either pass the string (character) containing the name "1" - "9", and convert it back to an integer, or (where necessary) you could pass a hash containing the names (bit indices) you wish to operate on.

I'm sure you can write the rest of the subroutines, if this is the route you decide to go.  (But feel free to ask if you need more help or suggestions!)


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: bit-wise by liverpole
in thread bit-wise by rir

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.