Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Building a byte to test truth table

by chromatic (Archbishop)
on Nov 02, 2000 at 01:52 UTC ( [id://39513]=note: print w/replies, xml ) Need Help??


in reply to Building a byte to test truth table

Perl supports something called bit vectors. Essentially, it's just a way to treat a string of bits as an arbitrary length byte.

You'd use something like $bv = pack('B*', $byte); to turn your string into a bit vector. You can use vec to access individual elements.

If you were to do that, it might simplify your code somewhat. The first regex could be replaced with:

if ((vec($bv, 0, 1) == 1) and (vec($bv, 4, 1) == 0)) { # do something }
That's untested, as I've not found a good use for this yet.

I'd probably turn your string into an integer and use bitwise operations to work on it.

Update: After studying this a bit more, it'll take more work to use vec to replace the regex. Use logical and, not, or, and xor as tye suggests. The bit-shifting operands will also come in handy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://39513]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found