Bitwise-Operator
Tue 09 December 2025
# 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: 5
Enter your second number: 9
Enter the number of bits to be shifted towards left and right: 2
Outcome of bitwise &: 1
Outcome of bitwise |: 13
Outcome of bitwise ^: 12
Outcome of bitwise ~: -6
Outcome of bitwise ~: -10
Outcome of bitwise <<: 20
Outcome of bitwise >>: 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: 4
Enter your second number: 2
Enter the number of bits to be shifted towards left and right: 9
Outcome of bitwise &: 0
Outcome of bitwise |: 6
Outcome of bitwise ^: 6
Outcome of bitwise ~: -5
Outcome of bitwise ~: -3
Outcome of bitwise <<: 2048
Outcome of bitwise >>: 0
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: 10
Enter the number of bits to be shifted towards left and right: 5
Outcome of bitwise &: 0
Outcome of bitwise |: 11
Outcome of bitwise ^: 11
Outcome of bitwise ~: -2
Outcome of bitwise ~: -11
Outcome of bitwise <<: 32
Outcome of bitwise >>: 0
Score: 50
Category: Python basics