Bitwise String Operators
Bitstrings of any size may be manipulated by the bitwise operators (~ | & ^
).
If the operands to a binary bitwise op are strings of different sizes, | and ^ ops act as though the shorter operand had additional zero bits on the right, while the & op acts as though the longer operand were truncated to the length of the shorter. The granularity for such extension or truncation is one or more bytes.
1 2 3 4 5 | # ASCII-based examples print "j p \n" ^ " a h" ; # prints "JAPH\n" print "JA" | " ph\n" ; # prints "japh\n" print "japh\nJunk" & '_____' ; # prints "JAPH\n"; print 'p N$' ^ " E<H\n" ; # prints "Perl\n"; |
If you are intending to manipulate bitstrings, be certain that you're supplying bitstrings: If an operand is a number, that will imply a numeric bitwise operation. You may explicitly show which type of operation you intend by using ""
or 0+
, as in the examples below.
1 2 3 4 5 6 7 | $foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF) $foo = '150' | 105; # yields 255 $foo = 150 | '105' ; # yields 255 $foo = '150' | '105' ; # yields string '155' (under ASCII) $baz = 0+ $foo & 0+ $bar ; # both ops explicitly numeric $biz = "$foo" ^ "$bar" ; # both ops explicitly stringy |
This somewhat unpredictable behavior can be avoided with the experimental "bitwise" feature, new in Perl 5.22. You can enable it via use feature
'bitwise'
. By default, it will warn unless the "experimental::bitwise"
warnings category has been disabled. (use experimental 'bitwise'
will enable the feature and disable the warning.) Under this feature, the four standard bitwise operators (~ | & ^
) are always numeric. Adding a dot after each operator (~. |. &. ^.
) forces it to treat its operands as strings:
1 2 3 4 5 6 7 8 9 10 11 12 | use experimental "bitwise" ; $foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF) $foo = '150' | 105; # yields 255 $foo = 150 | '105' ; # yields 255 $foo = '150' | '105' ; # yields 255 $foo = 150 |. 105; # yields string '155' $foo = '150' |. 105; # yields string '155' $foo = 150 |. '105' ; # yields string '155' $foo = '150' |. '105' ; # yields string '155' $baz = $foo & $bar ; # both operands numeric $biz = $foo ^. $bar ; # both operands stringy |
The assignment variants of these operators (&= |= ^= &.= |.= ^.=
) behave likewise under the feature.
The behavior of these operators is problematic (and subject to change) if either or both of the strings are encoded in UTF-8 (see Byte and Character Semantics in perlunicode.
See vec for information on how to manipulate individual bits in a bit vector.
Please login to continue.