# Python Bitwise Operators [Full Guide + Videos]

Bitwise operators perform operations on the binary (bit) representation of integers.

Background: Each integer is first written as a binary number that is a sequence of digits 0 or 1. For example:

• 0 is written as “0”
• 1 is written as “1”
• 2 is written as “10”
• 3 is written as “11”
• 4 is written as “100”
• 5 is written as “101”

The bitwise operators are then applied on those binary representations. The resulting binary number is then converted back to the integer representation (decimal number).

In the following table, we exemplify all bit-wise operators with variables `x` and `y` defined as follows:

```>>> x = 4  # 100
>>> y = 5  # 101```

As a comment, we also provide the binary representation `100` for the decimal integer `4`, and `101` for the decimal integer `5`.

Let’s dive into these bitwise operators one by one:

Python’s bitwise AND operator `x & y` performs logical AND on each bit position on the binary representations of integers `x` and `y`. Thus, each output bit is 1 if both input bits at the same position are 1, otherwise, it’s 0. For example, the integer expression 4 & 3 is translated to binaries 0100 & 0011 which results in 0000 because all four input bit positions are different.

In this example, you apply the bitwise AND operator to two integers 32 and 16:

```>>> 32 & 16
0```

The expression `32 & 16` operates on the bit representations `"010000"` (decimal 32) and `"001000"` (decimal 16) and performs bitwise AND. As all i-th bit positions are different, the result is 0:

For more examples and a more detailed explanation of the operator, check out our detailed blog guide.

Full Guide: Python Bitwise AND Operator

Python’s bitwise OR operator `x | y` performs logical OR on each bit position on the binary representations of integers `x` and `y`. Each output bit evaluates to 1 if and only if at least one of the two input bits at the same position are 1. For example, the integer expression `4 | 3` is translated to the binary operation `0100 | 0011` which results in `0111` because for the last three positions at least one bit is 1.

In this example, you apply the bitwise OR operator to two integers 32 and 16:

```>>> 32 | 16
48```

The expression `32 | 16` operates on the bit representations `"010000"` (decimal 32) and `"001000"` (decimal 16) and performs bitwise OR. Each `"1"` position propagates remains in the result `"110000"` (decimal 48):

For more examples and a more detailed explanation of the operator, check out our detailed blog guide.

Full Guide: Python Bitwise OR Operator

Python’s bitwise NOT operator `~x` inverts each bit from the binary representation of integer `x` so that 0 becomes 1 and 1 becomes 0. This is semantically the same as calculating `~x == -x-1`. For example, the bitwise NOT expression `~0` becomes `-1`, `~9` becomes `-10`, and `~32` becomes `-33`.

In this example, you apply the bitwise NOT operator to integer 32:

```>>> ~32
-33```

The expression `~32` operates on the bit representations `"0100000"` (decimal 32) and performs bitwise NOT resulting in binary `"1011111"`. This corresponds to the negative decimal number -33.

Latest articles

Related articles