|Perl Language Reference Manual|
by Larry Wall and others
Paperback (6"x9"), 724 pages
RRP £29.95 ($39.95)
Sales of this book support The Perl Foundation! Get a printed copy>>>
7.36 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.
# 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
0+, as in the examples below.
$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
|ISBN 9781906966027||Perl Language Reference Manual||See the print edition|