Bitwise-Operators
Wed 12 November 2025
# created : 20250113
# https://www.scientecheasy.com/2022/10/bitwise-operators-in-python.html/
# https://chatgpt.com/share/678524a9-cd24-8012-a475-2b7098cd8750
# Bitwise Operators in Python
# & Bitwise AND Binary
# | Bitwise OR Binary
# ^ Bitwise XOR (Exclusive OR) Binary
# ~ Bitwise NOT Unary
# << Bitwise Left Shift Binary
# >> Bitwise Right Shift Binary
# Bitwise AND operator (&)
A = 2
B = 6
result = A & B
print("Result of (2 & 6): ", result)
Result of (2 & 6): 2
A = 25
B = 45
result = A & B
print("Result of (25 & 45): ", result)
Result of (25 & 45): 9
A = -25
B = 45
result = A & B
print("Result of (-25 & 45): ", result)
Result of (-25 & 45): 37
X = True
Y = 45
result = X & Y
print("Result of (True & 10): ", result)
Result of (True & 10): 1
P = False
Q = 15
result = P & Q
print("Result of (False & 20): ", result)
Result of (False & 20): 0
# Bitwise OR operator (|)
A = 2
B = 6
result = A | B
print("Result of (2 | 6): ", result)
Result of (2 | 6): 6
A = 25
B = 45
result = A | B
print("Result of (25 | 45): ", result)
Result of (25 | 45): 61
A = -25
B = 45
result = A | B
print("Result of (-25 | 45): ", result)
Result of (-25 | 45): -17
X = True
Y = 45
result = X | Y
print("Result of (True | 10): ", result)
Result of (True | 10): 45
P = False
Q = 15
result = P | Q
print("Result of (False | 20): ", result)
Result of (False | 20): 15
# Bitwise XOR operator (^)
A = 2
B = 6
result = A ^ B
print("Result of (A ^ B): ", result)
Result of (A ^ B): 4
A = 25
B = 45
result = A ^ B
print("Result of (25 ^ 45): ", result)
Result of (25 ^ 45): 52
X = True
Y = 45
result = X ^ Y
print("Result of (True ^ 10): ", result)
Result of (True ^ 10): 44
P = False
Q = 15
result = P ^ Q
print("Result of (False ^ 20): ", result)
Result of (False ^ 20): 15
# Bitwise NOT operator (~)
A = 3
B = 1
result = A << B
print("Result of (A << B): ", result)
Result of (A << B): 6
X = 3
Y = 2
result = X << Y
print("Result of (X << Y): ", result)
Result of (X << Y): 12
X = 25
Y = 2
result = X << Y
print("Result of (A << B): ", result)
Result of (A << B): 100
# Bitwise Right Shift Operator (>>)
A = 6
B = 1
result = A >> B
print("Result of (A >> B): ", result)
Result of (A >> B): 3
X = 6
Y = 2
result = X >> Y
print("Result of (X >> Y): ", result)
Result of (X >> Y): 1
p = int(input('Enter your first number: '))
q = int(input('Enter your second number: '))
r = int(input('Enter the number of bits to be shifted towards left and right: '))
print('Outcome of bitwise &: ', p & q)
print('Outcome of bitwise |: ', p | q)
print('Outcome of bitwise ^: ', p ^ q)
print('Outcome of bitwise ~: ', ~p)
print('Outcome of bitwise ~: ', ~q)
print('Outcome of bitwise <<: ', p << r) ;print('Outcome of bitwise >>: ', p >> r)
Enter your first number: 1
Enter your second number: 2
Enter the number of bits to be shifted towards left and right: 3
Outcome of bitwise &: 0
Outcome of bitwise |: 3
Outcome of bitwise ^: 3
Outcome of bitwise ~: -2
Outcome of bitwise ~: -3
Outcome of bitwise <<: 8
Outcome of bitwise >>: 0
Score: 45
Category: python-basics