Java Reference
In-Depth Information
Bitwise AND
You can also use the bitwise AND operator, [
&
], which will convert both numbers into bin-
ary and returns a number that in binary has a
1
in each position for which the corresponding
bits of both operands are
1
s. Here's an example:
12 & 10; // in binary this is 1100 & 1010, so only the
first digit
↵
is 1 in both cases
<< 8
It can also be used with non-integers, where it returns
1
for true and
0
for false.
5 & "hello"; // both are true
<< 1
Bitwise OR
There is also the bitwise OR operator, [
|
], which will convert both numbers into binary and
return a number that in binary has a
1
in each position for which the corresponding bits of
either operands are
1
s. Here's an example:
12 | 10; // in binary this is 1100 & 1010, so the first 3
digits
↵
contain a 1
<< 14
This can also be used with non-integers, and returns
1
for true and
0
for false.
'' | "";
<< 0 // both are falsy
Bitwise XOR
Another operation is the bitwise XOR operator, [
^
], which stands for "eXclusive OR". This
will convert both numbers into binary and return a number that in binary has a
1
in each
