> ## 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.

# Bitwise Operators

> Explains Python bitwise operators, their behavior, examples, shifting, compound assignments, and two's complement implications for flags, masks, and low-level integer manipulation.

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.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/bitwise-operators-conjunction-disjunction-negation-exclusive.jpg?fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=1615abfac59a99e95e1d63b5a64d171c" alt="A dark-themed slide titled &#x22;Bitwise Operators&#x22; showing four black boxes with green symbols (&, |, ~, ^) labeled Conjunction, Disjunction, Negation, and Exclusive. The title is centered above the evenly spaced icons." width="1920" height="1080" data-path="images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/bitwise-operators-conjunction-disjunction-negation-exclusive.jpg" />
</Frame>

How each operator works:

| 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`                           |      |           |

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:

```python theme={null}
print(15 & 22)  # 6
```

The result 6 corresponds to binary 00000110.

Bitwise OR example (15 | 22)

Bitwise OR returns 1 for a bit position if either (or both) input bits are 1:

```python theme={null}
print(15 | 22)  # 31
```

The result 31 corresponds to binary 00011111.

Bitwise XOR example (15 ^ 22)

Bitwise XOR returns 1 only when exactly one of the bits is 1.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/exactly-1-xor-puzzle.jpg?fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=a8b3952fef65506eeaa20d2f23446d38" alt="A dark interface screen showing a puzzle titled &#x22;Exactly 1&#x22; with four colored boxes containing 0s and 1s connected by caret (^) symbols, suggesting exclusive choices. The boxes are outlined in red and green and centered beneath a small upward arrow." width="1920" height="1080" data-path="images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/exactly-1-xor-puzzle.jpg" />
</Frame>

```python theme={null}
print(15 ^ 22)  # 25
```

The result 25 corresponds to binary 00011001.

Bitwise NOT example (\~22)

Bitwise NOT flips every bit. In Python this yields the two's‑complement negative value; the identity \~n == -n - 1 holds for integers:

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/binary-ui-ones-green-zeros-red.jpg?fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=cfbc242a53b8a2256ff369fe78b86a4f" alt="A dark UI-style image showing two stacked black tiles on the left with green numbers &#x22;22&#x22; and &#x22;-23&#x22;, and two horizontal rows of small rounded tiles to the right containing 0s and 1s, where the 1s are highlighted green and the 0s red." width="1920" height="1080" data-path="images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/binary-ui-ones-green-zeros-red.jpg" />
</Frame>

```python theme={null}
print(~22)  # -23
```

Compound assignment forms

You can combine bitwise operations with assignment to update a variable in place. The long and abbreviated forms are equivalent:

```python theme={null}
# Long form
bit1 = bit1 & 22
bit1 = bit1 | 22
bit1 = bit1 ^ 22

# Abbreviated form
bit1 &= 22
bit1 |= 22
bit1 ^= 22
```

Bit shifting

Bit shifting moves bits left or right by a specified number of positions and is equivalent to multiplication or integer division by powers of two for non-negative integers.

* 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.

Examples with 22 (binary 10110):

```python theme={null}
print(22 >> 1)  # 11   (10110 >> 1 -> 1011)
print(22 >> 2)  # 5    (10110 >> 2 -> 101)
print(22 << 1)  # 44   (10110 << 1 -> 101100)
```

Equivalent arithmetic:

```python theme={null}
print(22 // 2)  # 11
print(22 >> 1)  # 11
print(22 // 4)  # 5
print(22 >> 2)  # 5
print(22 * 2)   # 44
print(22 << 1)  # 44
print(22 * 4)   # 88
print(22 << 2)  # 88
```

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/operators-logical-bitwise-shift-slide.jpg?fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=2817f4c7ea90c55295da64b5e164663f" alt="A dark presentation slide titled &#x22;Operators&#x22; with three bullet points explaining logical operators (and, not, or), bitwise operators (&, |, ^, ~) that return 0 or 1, and bit shifting using << and >>. The operator names are highlighted in different colors." data-og-width="1920" width="1920" data-og-height="1080" height="1080" data-path="images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/operators-logical-bitwise-shift-slide.jpg" data-optimize="true" data-opv="3" srcset="https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/operators-logical-bitwise-shift-slide.jpg?w=280&fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=2b99a40e992544c92aba2f00803b1cab 280w, https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/operators-logical-bitwise-shift-slide.jpg?w=560&fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=7763e336fe75835c41b32c5fc59a1276 560w, https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/operators-logical-bitwise-shift-slide.jpg?w=840&fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=7113518b54df2d5c236449ffdab93197 840w, https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/operators-logical-bitwise-shift-slide.jpg?w=1100&fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=e906b7486c0d22e261e312023428ab95 1100w, https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/operators-logical-bitwise-shift-slide.jpg?w=1650&fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=f4c3e032e89eeac6631addfcfaca4a40 1650w, https://mintcdn.com/kodekloud-c4ac6d9a/qTPiNmqXKGQjbUh5/images/Python-Basics/Logic-and-Bit-Operations/Bitwise-Operators/operators-logical-bitwise-shift-slide.jpg?w=2500&fit=max&auto=format&n=qTPiNmqXKGQjbUh5&q=85&s=7328776173b15bfd1dde53dccde1a628 2500w" />
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  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.
</Callout>

Recap

* 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.

Links and references

* [Python Numeric Types — bitwise operations](https://docs.python.org/3/library/stdtypes.html#bitwise-operations-on-integer-types)
* [Two's complement explanation (Wikipedia)](https://en.wikipedia.org/wiki/Two%27s_complement)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/python-basics/module/24b7c33f-d6b5-4346-9b97-739cf4a7e698/lesson/c69cf016-3d7e-42ac-9a20-3097cea2c14f" />
</CardGroup>
