Page 180 - AI Ver 3.0 Class 11
P. 180
Bitwise Operators
Bitwise operators perform bit-level operations on operands. They are used to manipulate individual bits of integers.
Operator Name Description
& Bitwise AND Returns bit 1, if both bits are 1; otherwise returns bit 0.
| Bitwise OR Returns bit 1, if any of the bits is 1; otherwise returns bit 0.
^ Bitwise XOR Returns bit 1, if any of the bits is 1 but not both; otherwise returns bit 0.
~ Bitwise NOT (complement) Inverts individual bits.
<< Bitwise left shift It moves all bits in a binary number to the left by a certain number of
positions.
>> Bitwise right shift It moves all bits in a binary number to the right by a certain number
of positions.
Program 9: To demonstrate the use of bitwise operators
# Uses of Bitwise Operators
# Bitwise AND
a = 10 # Binary: 1010
b = 6 # Binary: 0110
result_and = a & b
print("Bitwise AND:", result_and)
# Bitwise OR
result_or = a | b
print("Bitwise OR:", result_or)
# Bitwise XOR
result_xor = a ^ b
print("Bitwise XOR:", result_xor)
# Bitwise NOT
result_not = ~a
print("Bitwise NOT:", result_not)
# Bitwise Left Shift
result_left_shift = a << 2
print("Bitwise Left Shift:", result_left_shift)
# Bitwise Right Shift
result_right_shift = a >> 2
print("Bitwise Right Shift:", result_right_shift)
Output:
Bitwise AND: 2
Bitwise OR: 14
178 Touchpad Artificial Intelligence (Ver. 3.0)-XI

