We’re just going to cover the basics and have some fun with it.
Before anything else, what are bitwise operations? Well, they allow you to manipulate individual bits in a binary number. That might sound boring at first, but trust us there are plenty of cool applications for this!
For example, let’s say you have two numbers: `10` and `5`. If we perform the bitwise AND operation on them (using the `&` operator), we get a new number that has only the bits in common between the original two. In this case, that would be `2`, since both `10` and `5` have a 1 in the second position from the right:
# Example of bitwise AND operation using Python's & operator
# Declaring two numbers
num1 = 10 # First number
num2 = 5 # Second number
# Performing bitwise AND operation using & operator
result = num1 & num2 # Stores the result of the operation in the variable "result"
# Printing the binary representation of the result
print(bin(result)) # Output: '0b1000' (binary for decimal value 2)
# The binary representation of the result shows that only the bits in common between the two numbers are retained, resulting in a new number with a value of 2.
Now, let’s try the OR operation instead. This time, we get a new number that has a 1 in any position where either `10` or `5` had a 1. In other words, it combines the two original numbers:
# Example of bitwise OR operation using Python's | operator
# The bitwise OR operation combines two numbers by setting a 1 in any position where either number has a 1.
# In this example, we use the numbers 10 and 5, which have binary representations of '0b1010' and '0b0101' respectively.
# The result of the OR operation is a new number with a binary representation of '0b1011', which is equivalent to decimal value 13.
result = 10 | 5 # The | operator performs the bitwise OR operation on the two numbers and assigns the result to the variable 'result'.
print(bin(result)) # The bin() function converts the decimal value of 'result' to its binary representation and prints it.
# Output: '0b1011'
Finally, let’s look at the XOR operation. This one is a bit more interesting it returns a new number that has a 1 in any position where only one of `10` or `5` had a 1:
# Example of bitwise XOR (exclusive OR) operation using Python's ^ operator
# XOR operation returns a new number that has a 1 in any position where only one of `10` or `5` had a 1
# In this case, the binary representation of 10 is '1010' and the binary representation of 5 is '0101'
# The XOR operation compares each bit of the two numbers and returns a 1 if they are different, and a 0 if they are the same
# So, the result of 10 ^ 5 is '1111', which is the binary representation of decimal value 15
result = 10 ^ 5
# The bin() function converts the decimal value to its binary representation
# The output of bin() is a string, which is why it is enclosed in quotes
print(bin(result)) # Output: '0b1111' (binary for decimal value 15)
Of course, this is just the tip of the iceberg. There are plenty more operators and techniques out there that can help you manipulate binary data at its most granular level. But for now, let’s keep things simple and enjoy the ride!