What are Bitwise Operators in Java? Types, Examples and More

Bitwise operators in Java work directly on the individual bits of integer types like int and long. They're less commonly used in everyday application code but are essential for low-level tasks like flags, masks, and performance-critical operations.

1. Bitwise AND (&)

Compares each bit of two numbers and returns 1 only where both bits are 1.

int a = 6;  // 0110
int b = 5;  // 0101
int result = a & b; // 0100 = 4

2. Bitwise OR (|)

Compares each bit and returns 1 where at least one of the bits is 1.

int result = a | b; // 0111 = 7

3. Bitwise XOR (^)

Returns 1 where the bits are different, and 0 where they're the same.

int result = a ^ b; // 0011 = 3

4. Bitwise Complement (~)

Inverts every bit of the operand — 0 becomes 1 and 1 becomes 0 — and returns the two's-complement result.

int result = ~a; // -7

5. Left Shift (<<)

Shifts all bits to the left by a specified number of positions, filling the empty positions with 0. This effectively multiplies the number by 2 for each shift.

int result = 3 << 2; // 12 (3 * 2^2)

6. Right Shift (>>)

Shifts all bits to the right, preserving the sign bit. This effectively divides the number by 2 for each shift.

int result = 12 >> 2; // 3

7. Unsigned Right Shift (>>>)

Shifts bits to the right like >>, but always fills the leftmost bits with 0, ignoring the sign — useful when working with negative numbers as raw bit patterns.

Common Use Cases

  • Setting, clearing, or checking specific flags using bitmasks
  • Fast multiplication or division by powers of 2
  • Low-level operations in networking, graphics, and embedded systems
Bitwise operators only work with integer types in Java — they cannot be applied directly to float or double values.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course