in reply to Flipping bits and using flags

should be able to just use the & bitwise operator (see perlop) on the ascii (ord) value of the flag:
my $flag = "\r"; foreach my $bit ( 0 .. 4 ){ printf "Bit %d: %s\n", $bit+1, ( ord($flag) & 2**$bit ? "on" : "off" +); }

Replies are listed 'Best First'.
Re^2: Flipping bits and using flags
by GrandFather (Saint) on Mar 23, 2006 at 23:04 UTC

    I'd be inclined to use 1 << $bit instead. The result is exactly the same, but shift has bit semantics rather than the arithmetic semantics of **.

    For the OP's question ord is probably not required. The implication of the OP's post is that there is already a $flags variable so the test is:

    $flags & (1 << $bit) ? "on" : "off"

    It is important for OP to note that $bit ranges from 0 (least significant bit) to 4 (most significant of 5 bits) and that the expression 1 << $bit generates the values 1, 2, 4, 8 and 16.


    DWIM is Perl's answer to Gödel