
Bit Operators in Python: A Comprehensive Guide for You
Bit operators in Python are a powerful set of tools that allow you to manipulate individual bits within a number. These operators are particularly useful when working with binary data or when you need to perform low-level operations on integers. In this article, I will delve into the details of each bit operator, how they work, and when to use them. Let’s get started!
Bitwise AND (&)
The bitwise AND operator takes two numbers, converts them to their binary representation, and performs a logical AND operation on each pair of bits. The result is a new number that has bits set to 1 only where both bits in the original numbers were 1.
Here’s an example:
num1 = 12 Binary: 1100num2 = 15 Binary: 1111result = num1 & num2 Binary: 1100print(result) Output: 12
Bitwise OR (|)
The bitwise OR operator performs a logical OR operation on each pair of bits. The result is a new number that has bits set to 1 where at least one of the bits in the original numbers was 1.
Here’s an example:
num1 = 12 Binary: 1100num2 = 15 Binary: 1111result = num1 | num2 Binary: 1111print(result) Output: 15
Bitwise XOR (^)
The bitwise XOR operator performs a logical XOR operation on each pair of bits. The result is a new number that has bits set to 1 where the bits in the original numbers were different.
Here’s an example:
num1 = 12 Binary: 1100num2 = 15 Binary: 1111result = num1 ^ num2 Binary: 0011print(result) Output: 3
Bitwise NOT (~)
The bitwise NOT operator inverts each bit in the number. If a bit is 0, it becomes 1, and if a bit is 1, it becomes 0.
Here’s an example:
num = 12 Binary: 1100result = ~num Binary: 0011print(result) Output: -13 (assuming a signed integer representation)
Left Shift (<<)
The left shift operator shifts the bits of a number to the left by a specified number of positions. This is equivalent to multiplying the number by 2 raised to the power of the number of positions shifted.
Here’s an example:
num = 12 Binary: 1100result = num << 2 Binary: 110000print(result) Output: 48
Right Shift (>>)
The right shift operator shifts the bits of a number to the right by a specified number of positions. This is equivalent to dividing the number by 2 raised to the power of the number of positions shifted.
Here's an example:
num = 12 Binary: 1100result = num >> 2 Binary: 11print(result) Output: 3
Bitwise Operations with Strings
While bit operators are primarily used with integers, they can also be applied to strings. In Python, strings are represented as a sequence of bytes, so you can use bit operators on strings as well.
Here's an example:
str1 = "hello"str2 = "world"result = str1 & str2 Binary: 00000000000000000000000000000000print(result) Output: ''
As you can see, the result is an empty string because the bitwise AND operation between two strings results in an empty string.
Bitwise Operations with Lists
Bitwise operations can also be applied to lists. In this case, the operation is performed on each pair of corresponding elements in the lists.
Here's an example:
list1 = [1, 2, 3]list2