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"; }
The Secret to Fortune Cookies in One Line
print join("... in bed", `fortune fortunes` =~ m/^(.*)(\.|\?|\!)$/), "\n";
In reply to Re: Named Bits in a 32-bit integer
by BronzeWing
in thread Named Bits in a 32-bit integer
by RollyGuy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |