Bitwise operators let you manipulate individual bits of integer values by operating on their binary representations. These operators are commonly used for flags, masks, low-level data manipulation, and performance-sensitive code.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.

| Operator | Symbol | Description | Example | ||
|---|---|---|---|---|---|
| Bitwise AND | & | Returns 1 for each bit position where both operands have 1 | 15 & 22 → 6 | ||
| Bitwise OR | ` | ` | Returns 1 where at least one operand has 1 | `15 | 22→31` |
| Bitwise XOR | ^ | Returns 1 where exactly one operand has 1 (exclusive OR) | 15 ^ 22 → 25 | ||
| Bitwise NOT | ~ | Flips every bit. In Python this produces the two’s‑complement negative: ~n == -n - 1 | ~22 → -23 | ||
| Left shift | << | Moves bits left (multiplying non-negative integers by powers of two) | 22 << 1 → 44 | ||
| Right shift | >> | Moves bits right (dividing non-negative integers by powers of two using floor division) | 22 >> 1 → 11 |
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.