Bitwise operators act on bits (0s and 1s). They're used for low-level operations and efficient data manipulation in Python.
AND Operator (&)
& performs a bitwise AND. The result is 1 only if both bits are 1; otherwise, it's 0. Example: 5 & 3 is 0101 & 0011 = 0001 = 1.
OR Operator (|)
| performs a bitwise OR. The result is 1 if either bit is 1; it's 0 only if both bits are 0. Example: 5 | 3 is 0101 | 0011 = 0111 = 7.
XOR Operator (^)
^ performs a bitwise XOR (exclusive OR). The result is 1 if the bits are different, and 0 if they are the same. Example: 5 ^ 3 is 0101 ^ 0011 = 0110 = 6.
NOT Operator (~)
~ performs a bitwise NOT (inversion). It flips each bit. Example: ~5 is ~0101 = -6 (two's complement).
Left Shift (<<)
<< shifts bits to the left. It multiplies the number by 2 for each shift. Example: 5 << 1 is 0101 << 1 = 1010 = 10.
Right Shift (>>)
>> shifts bits to the right. It divides the number by 2 for each shift. Example: 5 >> 1 is 0101 >> 1 = 0010 = 2.