Bitwise operators and shifts
What you will learn
Bitwise operators work on the individual binary digits of integer values. They are useful for compact sets of flags and low-level data formats. Treat a flag as one bit in a number: setting it need not change the other bits.
public class Main { public static void main(String[] args) { int read = 0b001; int write = 0b010; int share = 0b100; int flags = read | share; System.out.println(flags); System.out.println((flags & write) != 0); System.out.println(flags & ~share); }}Expected output:
5false1The mask contains read and share. AND tests write; AND with the complemented share mask clears only share.
How it works
For integer operands, & keeps a bit when both operands have it set, | sets it if either has it set, ^ sets it when the two differ and ~ flips every bit of the promoted operand. For int, all 32 bits participate, so ~0 is -1, not a four-bit result. << shifts bits left and discards bits beyond the width. >> fills vacated high bits with the sign bit; >>> fills them with zeros. Shift operands are promoted individually; the left operand determines result width. An int uses the low five bits of the shift distance; a long uses the low six. Thus 1 << 32 equals 1, whereas 1L << 32 equals 4294967296. These operations do not validate flag positions. For a signed negative int, right shift and division may differ because division truncates toward zero, while signed right shift rounds toward negative infinity when viewed as division by a power of two.
public class Main { public static void main(String[] args) { int n = -8; System.out.println(n >> 1); System.out.println(n >>> 1); System.out.println(3 << 2); System.out.println(-3 >> 1); System.out.println(-3 / 2); }}Expected output:
-4214748364412-2-1The zero-fill result is positive for this negative int. Signed shifting differs from truncating division for negative odd numbers.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { int flags = 0b101; System.out.println(flags ^ 0b100); System.out.println(~0); System.out.println(1 << 32); System.out.println(1L << 32); }}Expected output:
1-114294967296XOR toggles the chosen bit. The other results demonstrate complement width and masked shift distance.
Common mistakes
Do not use flags == mask to test whether a particular bit is present when other flags may be set. Use (flags & mask) != 0 for one bit, or (flags & mask) == mask to test that every bit of a multi-bit mask is set. Use parentheses around bitwise operations before equality.
Try it yourself
Define three fictional feature flags 1, 2 and 4. Start with flags 1 and 2, enable flag 4, clear flag 2, and print the final mask and whether flag 2 remains set. Repeat the clear-and-check step when the starting mask is zero.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bitwise operators and shifts — knowledge check
Apply bitwise operators and shifts to examples and boundary conditions.
What is `0b101 & 0b011`?
Which expression toggles the bit represented by mask?
What is `1 << 32` for int?
Which operator shifts right with zero fill?
What is `~0` for int?