BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Numbers and math

Java language

Bitwise operators and flags

Bitwise operators work on the individual bits of a number: & and, | or, ^ xor, ~ not, << and >> shift.

Learn these first

At a glance

Mental model

A row of light switches.

What happens, in order
  1. 1Flags: int READ = 1, WRITE = 2, EXEC = 4; combine with perms = READ | WRITE, test with (perms & WRITE) != 0, remove with perms & ~WRITE
  2. 2x << n multiplies by 2^n, x >> n divides by 2^n keeping the sign, x >>> n shifts in zeros (unsigned)n & (n - 1) clears the lowest set bit; n & 1 tests for odd.
  3. 3^ is xor: a ^ a is 0, which powers swap tricks and "find the single number" puzzlesInteger.bitCount(n) counts the set bits; BitSet stores a big set of small non-negative ints compactly.
  4. 4On booleans & and | do not short-circuit while && and || do: a && b skips b when a is false, a & b always evaluates both
Remember
Watch out
Missing parentheses around & or | in a comparisonUsing >> on negative numbers when you wanted >>>1 << 31 overflows to a negative int
Example (highlighted lines explain it)
public class Permissions {  static final int READ = 1, WRITE = 1 << 1, EXEC = 1 << 2;  static boolean can(int perms, int flag) { return (perms & flag) != 0; }  static int grant(int perms, int flag) { return perms | flag; }  static int revoke(int perms, int flag) { return perms & ~flag; }  static boolean isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }  static int half(int n) { return n >>> 1; }}
Read the full explanation (about 2 min)

Bitwise operators work on the individual bits of a number: & and, | or, ^ xor, ~ not, << and >> shift. They pack flags into one int and enable fast tricks.

A mental model

A row of light switches. | flips chosen switches on, & checks which are on, ^ toggles them, and a shift moves the whole row one position along.

How it works

Flags: int READ = 1, WRITE = 2, EXEC = 4; combine with perms = READ | WRITE, test with (perms & WRITE) != 0, remove with perms & ~WRITE.

x << n multiplies by 2^n, x >> n divides by 2^n keeping the sign, x >>> n shifts in zeros (unsigned). n & (n - 1) clears the lowest set bit; n & 1 tests for odd.

^ is xor: a ^ a is 0, which powers swap tricks and "find the single number" puzzles. Integer.bitCount(n) counts the set bits; BitSet stores a big set of small non-negative ints compactly.

On booleans & and | do not short-circuit while && and || do: a && b skips b when a is false, a & b always evaluates both.

Going deeper

Operator precedence is a trap: x & 1 == 0 parses as x & (1 == 0) and does not compile; write (x & 1) == 0.

Shifting by 32 or more on an int shifts by n % 32, not to zero. Use long (1L << 40) for big masks.

Prefer EnumSet over int flags when the flags are an enum; the code reads like a normal set.

Under the hood

These operators map directly to CPU instructions and are the building blocks for hashing (h ^ (h >>> 16) in HashMap), compression and permissions in file systems.

Common mistakes

  • Missing parentheses around & or | in a comparison.
  • Using >> on negative numbers when you wanted >>>.
  • 1 << 31 overflows to a negative int.

How Unravel recognises it

When you paste code, Unravel looks for these signs of bitwise operators and flags. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Bitwise operators and flags?

Bitwise operators work on the individual bits of a number: & and, | or, ^ xor, ~ not, << and >> shift. They pack flags into one int and enable fast tricks.

What are the most common mistakes with Bitwise operators and flags?

Missing parentheses around & or | in a comparison. Using >> on negative numbers when you wanted >>>. 1 << 31 overflows to a negative int.

How can I see Bitwise operators and flags in real code?

Paste your own Java into Unravel and it will point out where bitwise operators and flags appears, explain how it works at the detail level you choose, and draw the flow of the code around it.