Sub topic 13
Bitwise and Non-Short-Circuit Boolean Operators
Java can use &, |, and ^ with either integer values or boolean values. Their meaning depends on the values you give them. With integers, they work on individual bits. With booleans, they combine true and false.
Work with bits
A bit is a single digit that is either 0 or 1. Binary is a way to write numbers using bits. In Java, a number starting with 0b is written in binary. For example, 0b1010 is the decimal number 10.
A bitwise operator compares or changes bits. The table shows how the three two-value operators work:
| First bit | Second bit | `&` AND | ` | ` OR | `^` XOR |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 0 | 1 | 1 | |
| 1 | 0 | 0 | 1 | 1 | |
| 1 | 1 | 1 | 1 | 0 |
AND gives 1 only when both bits are 1. OR gives 1 when either or both bits are 1. XOR gives 1 when the bits differ.
A mask is a number whose bits select positions you want to inspect or change. A flag is a bit used to represent an on-or-off setting. For example, 0b0010 selects the second bit from the right.
Save this program as BitwiseDemo.java, then compile and run it:
public class BitwiseDemo {
static boolean report() {
System.out.println("Right side ran");
return true;
}
public static void main(String[] args) {
int flags = 0b1010;
int mask = 0b0010;
System.out.println(flags & mask); // 2
System.out.println((flags & mask) != 0); // true
System.out.println(flags | 0b0001); // 11
System.out.println(flags ^ mask); // 8
System.out.println(flags & ~mask); // 8
System.out.println(false & report()); // false
System.out.println(false && report()); // false
}
}javac BitwiseDemo.java
java BitwiseDemoThe first result is 2, because only the 0b0010 bit is set in both numbers. The comparison checks whether that selected bit is on. OR turns on the last bit, while XOR flips the bit selected by the mask.
The ~ operator flips every bit of an integer: 0 becomes 1, and 1 becomes 0. In flags & ~mask, it helps clear the selected bit. An int has 32 bits, so ~ also flips bits not shown in the short binary number.
Use the operators with booleans
For boolean values, think of false as 0 and true as 1 in the table. For example, true ^ false is true, because the values differ.
With booleans, & and | are non-short-circuit operators. That means they evaluate both sides, even when the left side already determines the result. Java evaluates the left side first; if it fails with an exception, the right side does not run.
In the program, false & report() calls report(), so it prints Right side ran. But false && report() skips the call. The result is false in both cases.
Do not mix an integer and a boolean with these operators. Also, ~ works on integers, not booleans.
Practice
Exercise
If flags is 0b1010, what is flags | 0b0100 in decimal?
Show answer
14. OR turns on the selected bit, producing 0b1110.
Exercise
Does true | report() call report(), and what boolean result does it produce?
Show answer
Yes. The non-short-circuit | evaluates the right side, and the result is true.