DEV Community

RedouaneSarouf
RedouaneSarouf

Posted on

Python Bitwise Operators Explained in Detail

This post explains python bitwise in detail with easy to understand code example. Python provides several bitwise operators that allow you to work directly with the binary representations of integers.
We'll explore the world of Python bitwise operators and their functionality.

Understanding Bitwise Operators with code example:

Bitwise operators work on individual bits of integers, treating them as binary values (0s and 1s). Python provides the following bitwise operators:

  1. Bitwise AND (&):
    The & means both bits have to be on

        int1 = 10     # 00001010 in binary
                      # 00008020
        int2 = 6      # 00000110 in binary
                      # 00000420
    
        # int1        = 00001010 = 10
        # int2        = 00000110 = 6
        # int1 & int2 = 00000010 = 2
    
        print(10 & 6) # Output: 2 (00000010 in binary)
    
  2. Bitwise OR (|):
    The (|) means at least 1 bit has to be on or both.

        8bits(256 128 64 32 16 8 4 2 1)
        int1 = 10     # 00001010 in binary
                      # 00008020
        int2 = 6      # 00000110 in binary
                      # 00000420
    
        # int1        = 00000100 = 4
        # int2        = 00001000 = 8
        # int1 | int2 = 00001100 = 12
    
        print(4 & 8) # Output: 12 (00001100 in binary)
    
  3. Bitwise XOR (^):
    The ^ means both bits have to be different either (1 0) or (0 1)
    8bits(256 128 64 32 16 8 4 2 1)

        int1 = 10     # 00001010 in binary
                      # 00008020
        int2 = 6      # 00000110 in binary
                      # 00000420
        # int1        = 00000100 = 4
        # int2        = 00001000 = 8
        # int1 ^ int2 = 00001100 = 12
    
        print(4 & 8) # Output: 12 (00001100 in binary)
    
  4. Bitwise NOT (~): Flips the bits of an integer. It changes all 0s to 1s and all 1s to 0s.

        n = ~10  #  0000 1010
                  #  1111 0101        
        # the most significant bit is reserved for sign bit 
        # in this case the result will be negative 
        print(n)  # Output: -11 (Negative of (1010 + 1))
    
  5. Bitwise Left Shift (<<): Shifts the bits of the left operand to the left by a specified number of positions. This is equivalent to multiplying the number by 2 raised to the power of the shift count.

        5 << 2   
        # binary representation for 5 is 0000 0101
        # number 2 means shifting by 2 bits
        # << 2 means we shift 2 bits to the left 
        # 0001 0100
        print(5 >> 2)  # Output: 2 (0001 0100 in binary)
    
  6. Bitwise Right Shift (>>): Shifts the bits of the left operand to the right by a specified number of positions. This is equivalent to dividing the number by 2 raised to the power of the shift count, while discarding the remainder.

        5 >> 2   
        # binary representation for 5 is 0000 0101
        # number 2 means shifting by 2 bits
        # >> 2 means we shift 2 bits to the right 
        # 0000 0001
        print(5 >> 2)  # Output: 2 (0000 0001 in binary)
    

Top comments (0)