Bitwise Operators in Java are used to perform operations on binary representations of integers. These operators work at the bit level and include:
- AND (&): Performs a bitwise AND.
Example: 5 & 3 results in 1. - OR (|): Performs a bitwise OR.
Example: 5 | 3 results in 7. - XOR (^): Performs a bitwise exclusive OR.
Example: 5 ^ 3 results in 6. - NOT (~): Performs a bitwise complement (inverts bits).
Example: ~5 results in -6. - Left Shift (<<): Shifts bits to the left, adding zeros on the right.
Example: 5 << 2 results in 20. - Right Shift (>>): Shifts bits to the right, preserving the sign bit.
Example: 5 >> 1 results in 2. - Unsigned Right Shift (>>>): Shifts bits to the right, filling zeros irrespective of the sign.
Example: -5 >>> 1 results in 2147483645.