Python Bitwise Operators


Bitwise Operators in Python

Bitwise operators are used to perform operations on individual bits of integer values. They manipulate the bits directly and are often used in low-level programming, cryptography, and performance-critical applications. Python supports several bitwise operators, which operate on integers as their operands.

List of Bitwise Operators

OperatorDescriptionExampleResult
&Bitwise ANDa & bPerforms AND on each bit
``Bitwise OR`a
^Bitwise XOR (exclusive OR)a ^ bPerforms XOR on each bit
~Bitwise NOT (one's complement)~aInverts all bits
<<Left shifta << nShifts bits to the left by n positions
>>Right shifta >> nShifts bits to the right by n positions

Examples of Bitwise Operators

1. Bitwise AND (&)

The & operator compares each bit of two integers. If both bits are 1, the resulting bit is 1; otherwise, it's 0.

a = 5 # In binary: 0101 b = 3 # In binary: 0011 result = a & b # Result: 0001 (1 in decimal) print(result) # Output: 1

2. Bitwise OR (|)

The | operator compares each bit of two integers. If at least one of the bits is 1, the resulting bit is 1.

a = 5 # In binary: 0101 b = 3 # In binary: 0011 result = a | b # Result: 0111 (7 in decimal) print(result) # Output: 7

3. Bitwise XOR (^)

The ^ operator compares each bit of two integers. If the bits are different, the resulting bit is 1; if they are the same, it's 0.

a = 5 # In binary: 0101 b = 3 # In binary: 0011 result = a ^ b # Result: 0110 (6 in decimal) print(result) # Output: 6

4. Bitwise NOT (~)

The ~ operator inverts the bits of its operand. It changes 1 to 0 and 0 to 1.

a = 5 # In binary: 0101 result = ~a # Result: 1010 (in binary, representing -6 in two's complement) print(result) # Output: -6

5. Left Shift (<<)

The << operator shifts the bits of the left operand to the left by the number of positions specified by the right operand. This effectively multiplies the number by 2 for each shift.

a = 5 # In binary: 0101 result = a << 1 # Result: 1010 (10 in decimal) print(result) # Output: 10 result = a << 2 # Result: 010100 (20 in decimal) print(result) # Output: 20

6. Right Shift (>>)

The >> operator shifts the bits of the left operand to the right by the number of positions specified by the right operand. This effectively divides the number by 2 for each shift.

a = 20 # In binary: 10100 result = a >> 1 # Result: 01010 (10 in decimal) print(result) # Output: 10 result = a >> 2 # Result: 00101 (5 in decimal) print(result) # Output: 5

Summary of Bitwise Operators:

  • &: Bitwise AND. Returns a bit set to 1 only if both corresponding bits are 1.
  • |: Bitwise OR. Returns a bit set to 1 if at least one corresponding bit is 1.
  • ^: Bitwise XOR. Returns a bit set to 1 if corresponding bits are different.
  • ~: Bitwise NOT. Inverts all bits.
  • <<: Left shift. Shifts bits to the left, effectively multiplying by 2.
  • >>: Right shift. Shifts bits to the right, effectively dividing by 2.

Bitwise operators are useful for tasks that involve binary manipulation, such as low-level programming, network protocols, and image processing.