Python - Bitwise Operators

Hello there, future Python wizards! Today, we're going to embark on an exciting journey into the world of bitwise operators. Now, I know what you might be thinking: "Bitwise operators? That sounds like something only computer scientists care about!" But trust me, understanding these operators can be incredibly useful and even fun! So, let's dive in and unravel the mysteries of bits and bytes together.

Python - Bitwise Operators

Python Bitwise Operators

Bitwise operators are special operators that work on the binary representations of numbers. They manipulate individual bits, which are the smallest units of data in computing. Think of bits as tiny switches that can be either on (1) or off (0).

Before we delve into each operator, let's take a quick look at all the bitwise operators available in Python:

Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Left Shift Shifts left by pushing zeros in from the right
>> Right Shift Shifts right by pushing copies of the leftmost bit in from the left

Now, let's explore each of these operators in detail.

Python Bitwise AND Operator (&)

The Bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Let's look at an example:

a = 5  # binary: 0101
b = 3  # binary: 0011
result = a & b
print(f"{a} & {b} = {result}")  # Output: 5 & 3 = 1

In this example, we're performing a bitwise AND operation between 5 (0101 in binary) and 3 (0011 in binary). The result is 1 (0001 in binary).

Here's how it works:

  0101 (5)
& 0011 (3)
  ----
  0001 (1)

As you can see, only the rightmost bit is 1 in both numbers, so only that bit is 1 in the result.

Python Bitwise OR Operator (|)

The Bitwise OR operator (|) compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Here's an example:

a = 5  # binary: 0101
b = 3  # binary: 0011
result = a | b
print(f"{a} | {b} = {result}")  # Output: 5 | 3 = 7

In this case, we're performing a bitwise OR operation between 5 and 3. The result is 7 (0111 in binary).

Here's the breakdown:

  0101 (5)
| 0011 (3)
  ----
  0111 (7)

The result has a 1 wherever either of the original numbers had a 1.

Python Bitwise XOR Operator (^)

The Bitwise XOR (exclusive OR) operator (^) compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1. If the bits are the same, the corresponding result bit is set to 0.

Let's see an example:

a = 5  # binary: 0101
b = 3  # binary: 0011
result = a ^ b
print(f"{a} ^ {b} = {result}")  # Output: 5 ^ 3 = 6

Here, we're performing a bitwise XOR operation between 5 and 3. The result is 6 (0110 in binary).

Here's how it works:

  0101 (5)
^ 0011 (3)
  ----
  0110 (6)

The result has a 1 wherever the bits in the original numbers were different.

Python Bitwise NOT Operator (~)

The Bitwise NOT operator (~) is a unary operator (it only takes one operand) that flips the bits of its operand. Every 0 becomes 1, and every 1 becomes 0.

Here's an example:

a = 5  # binary: 0101
result = ~a
print(f"~{a} = {result}")  # Output: ~5 = -6

You might be wondering why the result is -6. This is because Python uses two's complement to represent negative numbers. In two's complement, the leftmost bit represents the sign (0 for positive, 1 for negative).

Here's what's happening under the hood:

 00000101 (5)
~
 11111010 (-6 in two's complement)

Python Bitwise Left Shift Operator (<<)

The Bitwise Left Shift operator (<<) shifts the bits of the first operand left by the number of places specified by the second operand. New bits on the right are filled with 0s.

Let's look at an example:

a = 5  # binary: 0101
b = 1
result = a << b
print(f"{a} << {b} = {result}")  # Output: 5 << 1 = 10

In this case, we're shifting the bits of 5 to the left by 1 position. The result is 10 (1010 in binary).

Here's what's happening:

0101 (5)
Shift left by 1
1010 (10)

Each left shift effectively multiplies the number by 2.

Python Bitwise Right Shift Operator (>>)

The Bitwise Right Shift operator (>>) shifts the bits of the first operand right by the number of places specified by the second operand. For positive numbers, new bits on the left are filled with 0s.

Here's an example:

a = 5  # binary: 0101
b = 1
result = a >> b
print(f"{a} >> {b} = {result}")  # Output: 5 >> 1 = 2

Here, we're shifting the bits of 5 to the right by 1 position. The result is 2 (0010 in binary).

Here's the breakdown:

0101 (5)
Shift right by 1
0010 (2)

Each right shift effectively divides the number by 2 (rounding down to the nearest integer).

And there you have it, folks! We've journeyed through the land of bitwise operators in Python. These operators might seem a bit abstract at first, but they're incredibly powerful tools, especially when you're working with low-level operations or need to optimize your code for performance.

Remember, practice makes perfect. Try playing around with these operators, experiment with different numbers, and see what results you get. Before you know it, you'll be manipulating bits like a pro!

Happy coding, and may the bits be ever in your favor!

Credits: Image by storyset