Bitwise operators are used to perform operations on individual bits of binary numbers. These operators are commonly used in computer programming for tasks such as data encryption, compression, and graphics processing. In this blog post, we will explore the basic concepts of bitwise operators and provide examples of how they can be used.
Binary Numbers
Before we dive into bitwise operators, it’s important to understand how binary numbers work. In the decimal number system, we use the digits 0-9 to represent numbers. However, in the binary number system, we use only two digits, 0 and 1, to represent numbers. Each digit in a binary number is called a bit. A collection of bits is used to represent a larger number.
For example, the decimal number 5 can be represented in binary as 101. The first digit (from the right) represents 2^0, the second digit represents 2^1, and the third digit represents 2^2. To convert a binary number to decimal, we simply multiply each digit by its corresponding power of 2 and add them together.
Bitwise Operators
Bitwise operators are used to perform operations on individual bits of binary numbers. There are six bitwise operators in Python:
- AND (&): This operator returns a 1 in each bit position where both operands have a 1. Otherwise, it returns 0.
- OR (|): This operator returns a 1 in each bit position where at least one operand has a 1. Otherwise, it returns 0.
- XOR (^): This operator returns a 1 in each bit position where the operands have different bits. Otherwise, it returns 0.
- NOT (~): This operator returns the complement of the operand. It changes each 1 to 0 and each 0 to 1.
- Left Shift (<<): This operator shifts the bits of the first operand to the left by the number of positions specified by the second operand. Zeros are added to the right side of the shifted bits.
- Right Shift (>>): This operator shifts the bits of the first operand to the right by the number of positions specified by the second operand. Zeros are added to the left side of the shifted bits.
Examples
Let’s look at some examples of how bitwise operators can be used.
AND (&)
The AND operator returns a 1 in each bit position where both operands have a 1. Otherwise, it returns 0.
1 2 3 4 | lessCopy codea = 0b10101010 b = 0b11110000 c = a & b print(bin(c)) # Output: 0b10100000 |
In this example, we use the AND operator to perform a bitwise AND operation on the binary numbers a
and b
. The result is stored in the variable c
. The output is 0b10100000
, which is the result of performing an AND operation on each pair of bits.
OR (|)
The OR operator returns a 1 in each bit position where at least one operand has a 1. Otherwise, it returns 0.
1 2 3 4 | lessCopy codea = 0b10101010 b = 0b11110000 c = a | b print(bin(c)) # Output: 0b11111010 |
In this example, we use the OR operator to perform a bitwise OR operation on the binary numbers a
and b
. The result is stored in the variable c
. The output is 0b11111010
, which is the result of performing an OR operation on each pair of bits.
XOR (^)
The XOR operator returns a 1 in each bit position where the operands have different bits. Otherwise, it returns