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)

Python bitwise operators

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.

  • Bitwise AND (&):

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)
  • Bitwise OR (|):

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)
  • Bitwise XOR (^):

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)
  • Bitwise NOT (~):

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.

  • Bitwise Left Shift (<<):

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
  • Bitwise Right Shift (>>):

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.

  1. Bitwise left and right shift operators in Python:

    • Description: Bitwise left shift (<<) and right shift (>>) operators shift the bits of a binary number to the left or right by a specified number of positions.
    • Code:
    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)
    
  2. Applying bitwise operators to binary numbers in Python:

    • Description: Bitwise operators (&, |, ^, ~) perform operations on individual bits of binary numbers.
    • Code:
    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)
    
  3. Using bitwise operators for low-level operations in Python:

    • Description: Bitwise operators are useful for low-level operations such as manipulating individual bits for tasks like setting, clearing, or toggling specific bits.
    • Code:
    flags = 0b1100
    # Set the third bit
    flags |= (1 << 2)
    # Clear the second bit
    flags &= ~(1 << 1)
    
  4. Bitwise operators and flags manipulation in Python:

    • Description: Flags in programming are often implemented using bitwise operators to represent multiple boolean states efficiently.
    • Code:
    READ = 0b001
    WRITE = 0b010
    EXECUTE = 0b100
    
    permissions = READ | WRITE  # Result: 0b011 (READ | WRITE)
    
  5. Combining bitwise operators for complex operations in Python:

    • Description: Bitwise operators can be combined to perform more complex operations such as extracting specific bits or swapping values.
    • Code:
    num = 0b11010110
    # Extract bits 3 to 6
    extracted_bits = (num >> 2) & 0b1111
    
  6. Bitwise operators vs logical operators in Python:

    • Description: Bitwise operators (&, |, ^, ~) operate on individual bits, while logical operators (and, or, not) work on entire values.
    • Code:
    a = 5
    b = 3
    
    bitwise_and = a & b  # Result: 1
    logical_and = a and b  # Result: 3 (truthy value)