Bitwise operators in Python

Norman 189 Published: 07/10/2024

Bitwise operators in Python

I'd be happy to explain bitwise operators in Python!

Bitwise operators are used to manipulate the individual bits of a binary number, which is represented by an integer value. Python has six main bitwise operators:

& (AND): This operator performs a bitwise AND operation on two numbers. It compares each bit of the first operand (memory-value) to the corresponding bit of the second operand (input-value). If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
# Python code for bitwise AND operation

a = 5 # binary: 101

b = 3 # binary: 011

print(a & b) # Output: 1 (binary: 001)

| (OR): This operator performs a bitwise OR operation on two numbers. It compares each bit of the first operand to the corresponding bit of the second operand. If either one or both bits are 1, the corresponding result bit is set to 1.
# Python code for bitwise OR operation

a = 5 # binary: 101

b = 3 # binary: 011

print(a | b) # Output: 7 (binary: 111)

^ (XOR): This operator performs a bitwise XOR operation on two numbers. It 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.
# Python code for bitwise XOR operation

a = 5 # binary: 101

b = 3 # binary: 011

print(a ^ b) # Output: 6 (binary: 110)

~ (NOT): This operator performs a bitwise NOT operation on a single number. It inverts all the bits of the given number.
# Python code for bitwise NOT operation

a = 5 # binary: 101

print(~a) # Output: -6 (binary: 1100110)

<< (LEFT SHIFT): This operator performs a left shift on a single number, which moves all the bits to the left and fills the vacated positions with zeros.
# Python code for bitwise LEFT SHIFT operation

a = 5 # binary: 101

print(a << 1) # Output: 10 (binary: 1010)

>> (RIGHT SHIFT): This operator performs a right shift on a single number, which moves all the bits to the right and fills the vacated positions with zeros.
# Python code for bitwise RIGHT SHIFT operation

a = 5 # binary: 101

print(a >> 1) # Output: 2 (binary: 010)

These are the basic bitwise operators in Python. They can be used to perform various operations on integers, such as setting or clearing bits, and they're commonly used in programming for tasks like creating masks, checking flags, and handling binary data.

I hope this explanation helps you understand bitwise operators in Python!

Assignment operators in Python

Assignment Operators in Python!

In Python, assignment operators are used to assign a value to a variable. They are represented by the = symbol followed by an operator. These operators perform specific operations on the assigned value. Let's dive into the world of assignment operators in Python!

1. Augmented Assignment Operators

Augmented assignment operators, also known as compound assignment operators, combine the assignment operation with a binary operation (like addition or subtraction). They are used to update the value of a variable by performing an operation on its current value.

Some examples:

x = 5  # initial value

x += 3 # x becomes 8 (x = x + 3)

print(x) # Output: 8

y = 2

y -= 1 # y becomes 1 (y = y - 1)

print(y) # Output: 1

z = 4

z *= 2 # z becomes 8 (z = z * 2)

print(z) # Output: 8

w = 6

w /= 3 # w becomes 2 (w = w / 3)

print(w) # Output: 2

2. Simple Assignment Operators

Simple assignment operators, also known as basic assignment operators, assign a value to a variable without performing any operations on it.

Some examples:

a = 10  # assigns 10 to a

print(a) # Output: 10

b = None # assigns None to b

print(b) # Output: None

3. In-Place Operators

In-place operators modify the original variable instead of creating a new one.

Some examples:

my_list = [1, 2, 3]

my_list.sort()

print(my_list) # Output: [1, 2, 3]

another_list = ['a', 'b', 'c']

another_list.append('d')

print(another_list) # Output: ['a', 'b', 'c', 'd']

In conclusion, Python's assignment operators provide a convenient way to perform various operations on variables. From simple assignments to augmented and in-place operations, these operators are essential for creating efficient and readable code.

Remember, there's no ++ or -- operator in Python! Instead, use the += or -= operators to increment or decrement values.