Bitwise operators in Python are used to perform bit-level operations on integers. These operators treat the operands as sequences of bits (0s and 1s) and perform operations on them at the bit level.
Bitwise Operators:
AND (&) – Performs a bitwise AND operation. The result is 1 if both bits are 1, otherwise 0.
x = 5 # 0101 in binaryy = 3 # 0011 in binary
result = x & y # 0001 in binary
print(result) # Output: 1
OR (|) – Performs a bitwise OR operation. The result is 1 if at least one bit is 1, otherwise 0.
x = 5 # 0101 in binaryy = 3 # 0011 in binary
result = x | y # 0111 in binary
print(result) # Output: 7
XOR (^) – Performs a bitwise XOR (exclusive OR) operation. The result is 1 if the bits are different, and 0 if they are the same.
x = 5 # 0101 in binaryy = 3 # 0011 in binary
result = x ^ y # 0110 in binary
print(result) # Output: 6
NOT (~) – Performs a bitwise NOT operation (also called bitwise complement). It inverts all the bits of the operand.
x = 5 # 0101 in binaryresult = ~x # Inverts all bits (two's complement)
print(result) # Output: -6 (because of two's complement representation)
Left Shift (<<) – Shifts the bits of the number to the left by a specified number of positions. This operation essentially multiplies the number by 2 for each position shifted.
x = 5 # 0101 in binaryresult = x << 1 # Shift left by 1 position
print(result) # Output: 10 (1010 in binary)
Right Shift (>>) – Shifts the bits of the number to the right by a specified number of positions. This operation essentially divides the number by 2 for each position shifted.
x = 5 # 0101 in binaryresult = x >> 1 # Shift right by 1 position
print(result) # Output: 2 (0010 in binary)
Examples:
Example 1: Using AND, OR, and XOR
x = 10 # 1010 in binary
y = 4 # 0100 in binary
# AND
result = x & y
print(result) # Output: 0 (0000 in binary)
# OR
result = x | y
print(result) # Output: 14 (1110 in binary)
# XOR
result = x ^ y
print(result) # Output: 14 (1110 in binary)
Example 2: Using NOT (bitwise complement)
x = 10 # 1010 in binary
result = ~x # Inverts all bits (two's complement)
print(result) # Output: -11 (because of two's complement)
Example 3: Using Left and Right Shift
x = 8 # 1000 in binary
# Left shift by 2 positions (multiply by 2^2 = 4)
result = x << 2
print(result) # Output: 32 (100000 in binary)
# Right shift by 1 position (divide by 2^1 = 2)
result = x >> 1
print(result) # Output: 4 (0100 in binary)
Truth Tables for Bitwise Operators:
AND (&)
OR (|)
XOR (^)
NOT (~) – Inverts all bits.Shift Left (<<) – Moves all bits to the left, filling the right side with zeros.
Shift Right (>>) – Moves all bits to the right, filling the left side with zeros (for positive numbers) or ones (for negative numbers).
Key Points:
Bitwise operators only work on integers and manipulate their binary representation.
Bitwise operations are often used in tasks like data encryption, network programming, and hardware-level programming.
Left shift increases the value of a number, while right shift decreases it.
Let me know if you need further clarification or examples! 😊
0 Comments