## Introduction to Bitwise Operators in Python

In the world of programming, operators are used for carrying out various mathematical or logical operations that aid in making the code more efficient and readable. One such type of operator is the bitwise operator that allows the programmer to manipulate the binary form of an integer.

In this article, we will take a deep dive into the definition, purpose, and implementation of the different types of bitwise operators in python. By the end of this article, you will have a clear understanding of how to use bitwise operators in your code and boost your programming efficiency.

## Definition and Purpose

Bitwise operators are used in python to manipulate the binary form of an integer. In simpler terms, it is used to perform operations on the individual bits that make up an integer.

An integer is represented in the binary form of 0s and 1s, where each bit represents a power of 2. For example, the integer 10 in binary form is 1010, where each position represents 2^3, 2^2, 2^1, and 2^0 respectively.

The purpose of bitwise operators is primarily to perform Boolean operations at the bit level, which is otherwise difficult to accomplish using regular operators. With bitwise operators, you can perform operations such as AND, OR, NOT, XOR, and many more.

We will take a closer look at each of these operators in the following sections.

## Types

Let’s now take a look at each type of bitwise operator and understand their functionality. Bitwise AND: The bitwise AND operator is represented by the symbol ‘&.’ When applied to two integers, it returns a new integer where the corresponding bits in both the numbers are 1.

For example, 12 & 25 would evaluate to 8. Binary Conversion:

12 -> 1100

25 -> 11001

When we apply ‘&’ to both the binary numbers, we get 1000, which is equal to 8.

Bitwise OR: The bitwise OR operator is represented by the symbol ‘|.’ When applied to two integers, it returns a new integer where any of the corresponding bits in either of the numbers is 1. For example, 12 | 25 would evaluate to 29.

Binary Conversion:

12 -> 1100

25 -> 11001

When we apply ‘|’ to both the binary numbers, we get 11101, which is equal to 29. Bitwise NOT: The bitwise NOT operator is represented by the symbol ‘~.’ When applied to an integer, it flips all the bits of the number, i.e, 0s become 1s and vice versa.

For example, ~12 would evaluate to -13. Binary Conversion:

12 -> 1100

When we apply ‘~’ to the binary number, we get 0011, which is 3.

Since the first bit of the result is 0, we know that the number is positive. Therefore, the decimal equivalent of 0011 is 3.

But, since we applied the NOT operator before the conversion, we negate the number, which gives us -13. Bitwise XOR: The bitwise XOR operator is represented by the symbol ‘^.’ When applied to two integers, it returns a new integer where only the corresponding bits in one of the numbers are 1.

For example, 12 ^ 25 would evaluate to 21. Binary Conversion:

12 -> 1100

25 -> 11001

When we apply ‘^’ to both the binary numbers, we get 10101, which is equal to 21.

Bitwise Right Shift: The bitwise right shift operator is represented by the symbol ‘>>.’ When applied to an integer, it shifts all the bits of the number to the right by the specified number of positions. For example, 12 >> 2 would evaluate to 3.

Binary Conversion:

12 -> 1100

When we apply ‘>>’ with shift value of 2, we get 11, which is 3. Bitwise Left Shift: The bitwise left shift operator is represented by the symbol ‘<<.' When applied to an integer, it shifts all the bits the number to the left by the specified number of positions.

For example, 12 << 2 would evaluate to 48. Binary Conversion:

12 -> 1100

When we apply ‘<<' with shift value of 2, we get 110000, which is 48.

## Functionality

Now that we have a clear understanding of the different types of bitwise operators, let’s take a look at how we can use the bitwise AND operator in our code. The bitwise AND operator is quite useful in scenarios where we need to check the presence of certain bits in an integer.

For example, suppose we have a binary number 101010, and we want to check if the second bit from the right is set to 1 or not. We can use the AND operator with the appropriate mask to achieve this.

101010 & 000100 = 0

In this example, we used the mask 000100 to check the second bit from the right. If the result is 0, it means that the bit is not set.

Conversely, if the result is non-zero, it means that the bit is set.

## Example

Let’s take a real-world example to understand how we can use bitwise AND in python. Suppose we have a list of integers, and we want to check if each number is divisible by 4 or not.

In python, we can use the bitwise AND operator along with the appropriate mask to achieve this. # list of integers

numbers = [12, 15, 20, 27, 36, 44]

# mask to check divisibility by 4

mask = 0b11

## for number in numbers:

if number & mask == 0:

print(number, “is divisible by 4”)

else:

print(number,”is not divisible by 4″)

In this example, we have used a mask of 0b11 to check if the last two bits of the number are 0 or not.

If the result of the AND operation is 0, it means that the number is divisible by 4.

## Conclusion

In conclusion, bitwise operators are a powerful tool in the python programming language that allow the programmer to manipulate the binary form of an integer. With the help of bitwise AND, bitwise OR, bitwise NOT, bitwise XOR, bitwise right shift, and bitwise left shift, the programmer can perform complex operations at the bit level with ease.

By employing the different types of bitwise operators in our code, we can significantly boost our programming efficiency while also making the code more readable and concise.

## Functionality

The Bitwise OR operator is used to perform a logical OR operation on each corresponding bit in two given integers. The operator is denoted by the vertical bar ‘|’ symbol.

When OR is performed on two bits, the resulting bit will be 1 if either of the corresponding bits is 1. The operation returns a new integer with the corresponding bits calculated.

For example, consider two integers 10 and 25 represented in binary notation as 01010 and 11001 respectively. A bitwise OR operation on these integers can be carried out as follows:

01010 | 11001 = 11011

Therefore the decimal equivalent of the resulting binary number 11011 is 27.

In Python, bitwise OR can be implemented using the ‘|’ symbol. Consider the following example:

num1 = 12

num2 = 25

result = num1 | num2

## print(result)

## The output will be:

## 29

## Example

Let’s look at a real-world example to understand how Bitwise OR can be used in Python. Suppose we have a list of favorite colors represented in binary format: green = 01101, blue = 01010, and red = 00101.

We want to find the favorite color that has been selected the most number of times. We can use the Bitwise OR operator to combine all the binary numbers into one number, with each bit representing if someone selected that color.

The resulting number will be the decimal representation of the binary number calculated by applying the Bitwise OR operation on all the numbers. green = 01101

blue = 01010

red = 00101

result = green | blue | red

## print(result)

## The output will be:

## 01111

## The decimal representation of the binary number

01111 is 15. Therefore, we can conclude that the favorite color selected the most number of times is a combination of green, blue, and red.

## Functionality

The Bitwise NOT operator is used to invert the bits of an integer. The operator is denoted by the tilde ‘~’ symbol.

The operation returns a new integer with the complement of the corresponding bits calculated. The complement of a binary number is a number that can be added to the original number to get all 1s in the bit representation.

When we calculate the complement of a number in binary format, each bit is replaced with its opposite value, i.e., 0s become 1s and 1s become 0s. Consider the following example:

num = 12

complement = ~num

The binary representation of num is 00001100.

The complement of num is 11110011. Therefore, the value of complement variable would be -13.

## Example

Let us consider a real-world scenario to understand how bitwise NOT can be employed in Python. Suppose we have a data file consisting of an ASCII-encoded HTML webpage and we need to transmit that file over a network.

However, the characters in the file are too big to be transmitted over the network. To make the data transfer more efficient, we can employ bitwise NOT to convert the ASCII characters to their character equivalents.

The ASCII standard defines all the printable characters that can be used to represent text in computers. The standard assigns a unique bit value or code for each character.

The first 127 ASCII characters are the same as the first 128 Unicode characters. However, the ASCII characters use only 7 bits for encoding, while Unicode uses 8 bits, which is why the ASCII characters are smaller in size.

In Python, we can use the chr() and ord() functions to convert characters to their corresponding ASCII code and back. The ord() function takes a character as input and returns the ASCII code for that character.

The chr() function takes an ASCII code and returns the corresponding character. Suppose we have an ASCII character ‘A’ and we want to compress it to a smaller size using bitwise NOT.

We can use the ord() function to get the ASCII code for character ‘A’, apply the bitwise NOT operator, then use the chr() function to convert it back to a character. char = ‘A’

ascii_code = ord(char)

compressed_code = ~ascii_code

compressed_char = chr(compressed_code)

## print(compressed_char)

## The output will be:

: (colon)

Therefore, we have compressed the character ‘A’ to a smaller size, and it can now be transmitted more efficiently over the network.

## Conclusion

In conclusion, bitwise operators are essential in programming languages such as Python to manipulate binary data. The Bitwise OR operator is used to perform logical OR operations on corresponding bits of two integers.

The result of the operations is a new integer that is calculated through the binary representation of the given integers. On the other hand, the Bitwise NOT operator is used to invert the bits of an integer.

It is commonly used in computing the complement of a binary number. By using the complement of a binary number, we can add it to the original number to get all 1s in the binary representation.

By understanding the functionality of these operators and how to use them in Python, programmers can significantly improve the efficiency and readability of their code.

## Functionality

The Bitwise XOR (exclusive OR) operator is used on each corresponding bit in two given integers. The operator is denoted by the caret ‘^’ symbol.

When XOR is performed on two bits, the resulting bit will be 1 if only one of the corresponding bits is 1. For example, consider two integers 5 and 3 represented in binary notation as 0101 and 0011 respectively.

## A bitwise XOR operation on these integers can be carried out as follows:

0101 ^ 0011 = 0110

The decimal equivalent of the binary number calculated by applying the Bitwise XOR operation on the given integers is 6. In Python, bitwise XOR can be implemented using the ‘^’ symbol.

## Consider the following example:

num1 = 12

num2 = 25

result = num1 ^ num2

## print(result)

## The output will be:

## 21

## Example

Let’s look at a real-world example to understand how Bitwise XOR can be used in Python. Suppose we have a list of daily expenses as binary numbers for each day.

We want to find the total amount that was spent on weekdays, i.e., from Monday to Friday. We can use the Bitwise XOR operator to determine if each day was a weekday.

We can consider the weekdays as 1 and weekends as 0 in the binary representation, and XOR the binary numbers for each day to calculate the total spending on weekdays. binary_monday = 10010

binary_tuesday = 10100

binary_wednesday = 10110

binary_thursday = 11100

binary_friday = 10101

binary_saturday = 11011

binary_sunday = 10111

result = binary_monday ^ binary_tuesday ^ binary_wednesday ^ binary_thursday ^ binary_friday

## print(result)

## The output will be:

## 01001

## The decimal representation of the binary number

01001 is 9. Therefore, we can conclude that the total expenses on weekdays were $9.

## Functionality

Bitwise Shift Operators are used to shift the bits of an integer left or right by a specified number of positions. Shifting the bits to the left multiplies the integer by 2 to the power of the number of positions shifted, and shifting the bits to the right divides the integer by 2 to the power of the number of positions shifted.

The operators are denoted by the ‘>>’ symbol for right shift and ‘<<' symbol for left shift.

## Bitwise Right Shift Operator

The Bitwise Right Shift operator moves the bits of an integer to the right by a specified number of positions, discarding the least significant bits. The operator is denoted by the ‘>>’ symbol.

For example, consider the binary number 11001010, which represents 202 in decimal. If we shift the bits to the right by two positions, we get the binary number 00110010, which represents 50 in decimal.

This operation is equivalent to dividing the original integer by 2 raised to the power of the number of positions shifted. In Python, bitwise right shift can be implemented using the ‘>>’ symbol.

## Consider the following example:

num = 12

result = num >> 2

## print(result)

## The output will be:

## 3

## Bitwise Left Shift Operator

The Bitwise Left Shift operator moves the bits of an integer to the left by a specified number of positions, adding zeros to the least significant bits. The operator is denoted by the ‘<<' symbol.

For example, consider the binary number 01000101, which represents 69 in decimal. If we shift the bits to the left by two positions, we get the binary number 00010100, which represents 20 in decimal.

This operation is equivalent to multiplying the original integer by 2 raised to the power of the number of positions shifted. In Python, bitwise left shift can be implemented using the ‘<<' symbol.

## Consider the following example:

num = 12

result = num << 2

## print(result)

## The output will be:

## 48

## Conclusion

In conclusion, we have discussed two new Bitwise Operators – XOR and Shift operators. The XOR operator is used to perform exclusive OR operations on each corresponding bit of two integers.

The bitwise shift operators- right shift and left shift- are used for shifting the bits of an integer left or right by a specified number of positions..