Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Named Bits in a 32-bit integer

by BronzeWing (Monk)
on Jul 31, 2002 at 19:27 UTC ( #186568=note: print w/replies, xml ) Need Help??

in reply to Named Bits in a 32-bit integer

I think the simplest way to do it is with the &, |, ^, and ~ operators. An example:

#!/usr/bin/perl use warnings; use strict; use constant EXECUTE => 1; use constant WRITE => 2; use constant READ => 4; my $Permissions = 0; $Permissions |= READ; # Set READ to true $Permissions |= WRITE; # Set WRITE to true $Permissions &= ~READ; # Set READ to false $Permissions ^= EXECUTE; # Toggle EXECUTE print $Permissions, "\n";

Prints "3" (that's WRITE and EXECUTE).
Is this what you were looking for?


Oops! I completely forgot getting the info back out again. To check the status of a bit:

if ($Permissions & READ) { print "You can read.\n"; } else { print "You can't read.\n"; }

Just like that.

The Secret to Fortune Cookies in One Line
print join("... in bed", `fortune fortunes` =~ m/^(.*)(\.|\?|\!)$/), "\n";

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2023-01-30 07:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found