Bitwise operators in Python are used to perform operations on individual bits of binary numbers. These operators include bitwise AND, OR, XOR, and more.
- Bitwise AND (&): Performs a bitwise AND operation on the binary representations of the operands.
- Bitwise OR (|): Performs a bitwise OR operation.
- Bitwise XOR (^): Performs a bitwise XOR operation.
- Bitwise NOT (~): Flips the bits of the operand, changing 0 to 1 and 1 to 0.
- Left Shift (<<): Shifts the bits to the left by a specified number of positions.
- Right Shift (>>): Shifts the bits to the right.
a = 5 # Binary: 0101
b = 3 # Binary: 0011
result = a & b # Result: 0001 (Decimal: 1)x = 10 # Binary: 1010
y = 7 # Binary: 0111
result = x | y # Result: 1111 (Decimal: 15)