As others have mentioned, what you want is probably vec, but you'll have to be careful how you use it. Here's an example on how to convert from your regular input array to a hash of binary sequences. As a note, 0bN is the binary value "N", just like 0xN is the hexadecimal one:
my @slot = qw[ A A B B C C B B ]; my %slot_mask; # Create a hash based on the input names. Each hash entry # contains a single binary sequence for the "slot" numbers # that are specified earliner in @slot foreach (0..$#slot) { # Create a "blank" scalar if none is defined already unless (defined($slot_mask{$slot[$_]}) { $slot_mask{$slot[$_]} = ''; } # Set this particular bit to 1 using an assignment vec($slot_mask{$slot[$_]}, $_, 1) = 1; } # Retrieval, as per your example, for the 1st slot if ($slot_mask{A} & 0b00000010) { print "1st is A...\n"; } # Same idea but with the shift operator that is less prone # to typographical errors if ($slot_mask{A} & (1 << 1)) { print "1st is A...\n"; } # And likewise for the 4th or what have you, but this time # using the vec() function if (vec($slot_mask{A}, 4, 1)) { print "4th is A...\n"; }
I'm not sure of what the utility of such a thing would be, though. You might be chasing a data structure with little practical value. This is probably what you mean by "trying too hard."

As a note, comparing to 0bN values is probably a bad idea if you have a large number of "slots", such as more than 31, as you'll be using numbers outside the typical range of your run-of-the-mill 32-bit "int".

As a note, in your example you specified A being 0b11000000, which is technically backwards. It is actually 0b00000011 since the 0th slot is on the conceptual right side, not the left, when counting bits.

In reply to Re: Binary data type? by tadman
in thread Binary data type? by KPeter0314

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.