Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Bitwise operators in Python are used to perform operations on integers at the bit level. These operators are useful when you need to manipulate individual bits of a number, such as when working with binary data or low-level programming tasks.
In this tutorial, we will explore the different bitwise operators in Python and how to use them.
&
):The bitwise AND operator compares each bit of the first integer to the corresponding bit of the second integer. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.
a = 10 # Binary: 1010 b = 7 # Binary: 0111 result = a & b print(result) # Output: 2 (Binary: 0010)
|
):The bitwise OR operator compares each bit of the first integer to the corresponding bit of the second integer. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.
a = 10 # Binary: 1010 b = 7 # Binary: 0111 result = a | b print(result) # Output: 15 (Binary: 1111)
^
):The bitwise XOR operator compares each bit of the first integer to the corresponding bit of the second integer. If the bits are different, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.
a = 10 # Binary: 1010 b = 7 # Binary: 0111 result = a ^ b print(result) # Output: 13 (Binary: 1101)
~
):The bitwise NOT operator inverts all the bits of the integer. It flips 1s to 0s and 0s to 1s.
a = 10 # Binary: 0000 1010 result = ~a # Binary: 1111 0101 print(result) # Output: -11
Note that the result of the bitwise NOT operator is negative due to the way Python represents negative integers using two's complement.
<<
):The bitwise left shift operator shifts the bits of the integer to the left by a specified number of positions. Zeros are shifted in from the right.
a = 10 # Binary: 0000 1010 result = a << 2 # Binary: 0010 1000 print(result) # Output: 40
>>
):The bitwise right shift operator shifts the bits of the integer to the right by a specified number of positions. If the integer is unsigned, zeros are shifted in from the left. If the integer is signed, the sign bit is shifted in from the left.
a = 10 # Binary: 0000 1010 result = a >> 2 # Binary: 0000 0010 print(result) # Output: 2
In conclusion, bitwise operators in Python are used to perform operations on integers at the bit level. They provide a way to manipulate individual bits of a number and are useful when working with binary data or low-level programming tasks. Keep in mind that these operators should be used with integers only, as using them with other data types can lead to unexpected results.
Bitwise left and right shift operators in Python:
<<
) and right shift (>>
) operators shift the bits of a binary number to the left or right by a specified number of positions.num = 5 left_shifted = num << 1 # Result: 10 (binary 101 shifted left by 1 position) right_shifted = num >> 1 # Result: 2 (binary 101 shifted right by 1 position)
Applying bitwise operators to binary numbers in Python:
&
, |
, ^
, ~
) perform operations on individual bits of binary numbers.a = 5 # binary 101 b = 3 # binary 011 bitwise_and = a & b # Result: 1 (binary 001) bitwise_or = a | b # Result: 7 (binary 111) bitwise_xor = a ^ b # Result: 6 (binary 110) bitwise_not_a = ~a # Result: -6 (binary 1111111111111111111111111111111111111111111111111111111111111011)
Using bitwise operators for low-level operations in Python:
flags = 0b1100 # Set the third bit flags |= (1 << 2) # Clear the second bit flags &= ~(1 << 1)
Bitwise operators and flags manipulation in Python:
READ = 0b001 WRITE = 0b010 EXECUTE = 0b100 permissions = READ | WRITE # Result: 0b011 (READ | WRITE)
Combining bitwise operators for complex operations in Python:
num = 0b11010110 # Extract bits 3 to 6 extracted_bits = (num >> 2) & 0b1111
Bitwise operators vs logical operators in Python:
&
, |
, ^
, ~
) operate on individual bits, while logical operators (and
, or
, not
) work on entire values.a = 5 b = 3 bitwise_and = a & b # Result: 1 logical_and = a and b # Result: 3 (truthy value)