
Note: Bitwise operators operate on integers only; they are not defined for floating-point numbers in Python. For official behavior and details, see the Python documentation on numeric types and bitwise operations.
Bitwise AND example (15 & 22)
15 in binary: 00001111
22 in binary: 00010110 Bitwise AND compares each corresponding bit and returns 1 only where both bits are 1:


- Right shift (
>>): shifts bits to the right. Each shift right by 1 divides the integer by 2 using floor division for non-negative values. - Left shift (
<<): shifts bits to the left. Each shift left by 1 multiplies the integer by 2.

Bitwise operators are useful for flags, masks, and efficient low-level manipulations. Remember that Python integers are unbounded and bitwise operations follow two’s‑complement logic, so ~n equals -n - 1.
- Logical operators (
and,or,not) operate on boolean expressions and return boolean results — useful for control flow and conditions. - Bitwise operators (
&,|,^,~) operate at the bit level on integers and return integer results reflecting bitwise changes. - Bit shifts (
<<,>>) move bits left or right and correspond to multiplication or integer division by powers of two for non-negative integers.