# Advanced Java/Bitwise Operators
## Short-Circuit Evaluation

It should be noted that, when given a boolean logical operator, the JVM checks as few operands as possible, from left to right. For example, if the first operator of an OR expression is true, then the expression will be true no matter what the second operand is. Short-circuiting applies to the AND and OR operators.
This is called short-circuit evalution and is important if the second operand is a method call. Consider the following methods:
If the go method is called, the output is
Why? The JVM hits the if statement and looks at the first operand — here, the true literal. The expression could still go either way: for an AND to be true, both operands must be true. So the JVM checks the second operand; since it's a method call, the method is called in its entirety.
What if the first operand was the false literal?
The JVM sees that the first operand is false and immediately evaluates the entire AND expression as false. (Remember, an AND expression is only true if both operands are true.) Because the second operand is never checked, the method is never called, and the output is

## Long-Circuit Evaluation

Usually, short-circuit evaluation is a good thing, because it saves running time. But if the second operand is a method call that changes something before returning a value, you might want to force the method to run no matter what. To do this, use the long-circuit logical operators. They're the same as the bitwise operators, but they operate logically when applied to booleans. They're exactly the same as the short-circuit boolean operators, only not doubled.
| | Short-circuit | Long-circuit |
| AND | && | & |
| OR | <nowiki>||</nowiki> | <nowiki>|</nowiki> |
| XOR | none | ^ |
Note that there is no short-circuit form of the XOR operator, because, mathematically, it always needs to evaluate both its arguments to compute its result.
If you use a bitwise operator in a boolean expression, both operands are always evaluated. Consider the above code again. If the go method is rewritten:
Then the output will be:
Because the showFalse method will be called even though the first operand (the false literal) alone determines the result of the entire expression.

## Bitwise Operations

The other use of the bitwise operators is manipulating individual bits in an int. (Note that the operands can be any integral type; but if it is a type smaller than int, it will be promoted to an int type, and the result will be int.)
For the purposes of this section, we'll define the following variables:
The Bitwise Operators :
| Operator | Name |
| ~ | Bitwise NOT |
| & | Bitwise AND |
| <nowiki>|</nowiki> | Bitwise OR |
| ^ | Bitwise XOR |
| << | Left-Shift |
| >> | Right-Shift (Keep bits) |
| >>> | Right-Shift (Force bits) |

### Bitwise NOT

The bitwise NOT is a unary operator (it takes only one operand) that flips every bit in its operand.

### Bitwise AND

The result of a bitwise AND has on-bits (ones) only where both operands has an on-bit.

### Bitwise OR

The result of a bitwise OR has on-bits where either (or both) of its operands has an on-bit.

### Bitwise XOR

The result of a bitwise XOR (eXclusive-OR) has on-bits where one and only one of its operands has an on-bit.

### Left Shift

The left-shift operator shifts the bits of the first operand left by the number of places specified by the second operand. Bits shoved off the left side are lost; empty bits on the right are filled in with zeros.

### Right Shift (Keep)

The right-shift operator (keep bits) shifts the bits of the first operand right by the number of places specified by the second operand. Bits shoved off the right side are lost; bits on the left side are the same as the original left-most bit (the "sign bit").

### Right Shift (Force)

The right-shift operator (force bits) is the same as the right-shift (keep) operator. However, the new bits on the left are always set to zero.
* Project: Advanced Java
* Previous: Declaring float and long Literals — Advanced Java/Bitwise Operators — Next: java.util.List