Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found