I can't see any major problem — only a minor one, which is that [$bitmask] should most likely have been [$bitmask_value] :)
In other words, & is the correct operator to test if certain bits are set.
In case you want to test if more than one bit is set, you should check if the masked value equals the mask, not only if it's true, i.e.
if (($value & $mask) == $mask) ...
Example:
my $mask = 0b00001110; # test if bits 2,3,4 are set check($_, $mask) for 0b01101110, 0b01100110; sub check { my ($value, $mask) = @_; printf " val %08b\n", $value; printf "mask %08b\n", $mask; print " & --------\n"; printf " %08b ", $value & $mask; if ( ($value & $mask) == $mask ) { # equals mask? print "=> all tested bits set"; } elsif ( $value & $mask ) { # just true? print "=> some tested bits set"; } print "\n\n"; } __END__ val 01101110 mask 00001110 & -------- 00001110 => all tested bits set val 01100110 mask 00001110 & -------- 00000110 => some tested bits set
In reply to Re: bitmask check
by Eliya
in thread bitmask check
by wwe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |