my $flag = "\r"; foreach my $bit ( 0 .. 4 ){ printf "Bit %d: %s\n", $bit+1, ( ord($flag) & 2**$bit ? "on" : "off"); }