Bitwise Operators in Python | A Complete Guide with Examples

Bitwise Operators in Python | A Complete Guide with Examples

Bitwise Operators in Python

Bitwise operators in Python are powerful tools that perform operations at the binary level. They manipulate the individual bits of an integer, making them useful for tasks like encryption, data compression, and memory optimization. This article explains each bitwise operator in Python, along with examples, applications, and visual aids.

What Are Bitwise Operators?

Bitwise operators operate on the binary representations of integers. They enable operations like AND, OR, XOR, and NOT at the bit level. While not commonly used in high-level programming, they are crucial for specific use cases such as:
  • Encryption: Enhancing data security.
  • Data compression: Reducing storage requirements.
  • Memory optimization: Packing data into bits to save memory.
  • Low-level programming: Direct hardware interactions.
Let’s dive into the details of each operator.

List of Bitwise Operators in Python

Python supports six bitwise operators:
OperatorDescriptionSymbol
ANDSets each bit to 1 if both bits are 1&
ORSets each bit to 1 if at least one bit is 1`
XORSets each bit to 1 if the bits are different^
NOTInverts all the bits~
Left ShiftShifts bits to the left<<
Right ShiftShifts bits to the right>>

Examples and Explanations

1. Bitwise AND (&)

The AND operator performs a logical multiplication, setting a bit to 1 only if both bits are 1.

Truth Table:

ABA & B
000
010
100
111

Code Example:

python
a = 4 # Binary: 0100 b = 5 # Binary: 0101 print(a & b) # Output: 4 (Binary: 0100)

Explanation:

The operation proceeds bit by bit:
scss
0100 (a) & 0101 (b) ----- 0100 (result)

2. Bitwise OR (|)

The OR operator performs a logical addition, setting a bit to 1 if either bit is 1.

Truth Table:

| A | B | A | B | |——-|——-|——–| | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 1 |

Code Example:

python
a = 4 # Binary: 0100 b = 5 # Binary: 0101 print(a | b) # Output: 5 (Binary: 0101)

3. Bitwise XOR (^)

The XOR operator sets a bit to 1 if the corresponding bits of the operands are different.

Truth Table:

ABA ^ B
000
011
101
110

Code Example:

python
a = 4 # Binary: 0100 b = 5 # Binary: 0101 print(a ^ b) # Output: 1 (Binary: 0001)

4. Bitwise NOT (~)

The NOT operator flips all the bits in the binary representation of a number, converting 1s to 0s and vice versa.

Code Example:

python
a = 4 # Binary: 0100 print(~a) # Output: -5

Explanation:

  • Convert the value to binary (0100).
  • Take the complement (1011).
  • Convert the result to a signed integer using two’s complement, resulting in -5.

5. Bitwise Right Shift (>>)

The right shift operator moves all bits to the right by a specified number of positions, effectively dividing the value by 2n2^n, where nn is the number of shifts.

Code Example:

python
a = 7 # Binary: 0111 print(a >> 1) # Output: 3 (Binary: 0011)

6. Bitwise Left Shift (<<)

The left shift operator moves all bits to the left by a specified number of positions, effectively multiplying the value by 2n2^n.

Code Example:

python
a = 7 # Binary: 0111 print(a << 1) # Output: 14 (Binary: 1110)

Precedence of Bitwise Operators in Python

The precedence of bitwise operators, from highest to lowest, is as follows:
  1. ~
  2. &
  3. ^
  4. |
  5. << and >>

FAQs About Bitwise Operators

1. What is the bitwise complement operator in Python?

The bitwise NOT operator (~) is also known as the complement operator, as it complements all bits of a number.

2. Can bitwise operators be used with floating-point numbers?

No, bitwise operators can only be used with integers.

3. What’s the difference between bitwise AND and logical AND?

  • Bitwise AND (&) operates on binary values and returns an integer.
  • Logical AND (and) operates on Boolean values and returns True or False.

CONCLUSION

Understanding bitwise operators is essential for tasks requiring low-level programming or optimization. Their efficiency in manipulating data at the binary level makes them invaluable in fields like cryptography and data compression. Click Here to know more our program!