Java Reference

In-Depth Information

position for which the corresponding bits of either operands are 1s, but not both
1
s. Here's

an example:

12 ^ 10; // in binary this is 1100 & 1010, so only the

second and

↵
third digits are exclusively 1s

<< 6

When using non-integer values, this evaluates to
1
if
either
operands are truthy and evalu-

ates to
0
if both operands are truthy or both are falsy:

1 ^ 0; // The first operand is truthy

<< 1

true ^ true; // if both operands are true then the result

is false

<< 0

Bitwise Shift Operators

The bitwise shift operators,
<<
and >>, will move the binary representation a given number

of places to the right or left, which effectively multiplies or divides the number by powers

of two:

3 << 1; // multiply by 2

<< 6

16 >> 1; // divide by 2

<< 8

5 << 3; multiply by 2 cubed (8)

<< 40