Bitwise Operators
Like C, Perl has bitwise and, or, xor
(exclusive or), and not operators: &, |, ^, and the
previously described ~. You’ll have
noticed from your painstaking examination of the table at the start of
this chapter that bitwise and has a
higher precedence than the others, but we’ve cheated and combined them in
this discussion.
These operators work differently on numeric values than they do on
strings. (This is one of the few places where Perl cares about the
difference.) If either operand is a number (or has been used as a number),
both operands are converted to integers, and the bitwise operation is
performed between the two integers. These integers are guaranteed to be at
least 32 bits long, but they can be 64 bits on some machines. The point is
that there’s an arbitrary limit imposed by the machine’s architecture. You
can get around this restriction with the bigint pragma.
If both operands are strings (and have not been used as numbers since they were set), the operators do bitwise operations between corresponding bits from the two strings. In this case, there’s no arbitrary limit, since strings aren’t arbitrarily limited in size. If one string is longer than the other, the shorter string is considered to have a sufficient number of 0 bits on the end to make up the difference. Bits in each corresponding logical character in the two strings are and’d, or’d, or xor’d together.
For example, if you and together two strings:
"123.45" & "234.56"
you get another string:
"020.44" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access