http://qs1969.pair.com?node_id=186568


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?

-BronzeWing

Update:
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";