Bit Manipulation A Level
Bit Manipulation A Level
Definition:
Bit manipulation refers to the process of directly working with individual bits (0s and 1s) within a
binary number. This is done using bitwise operators such as AND, OR, XOR, NOT, shifts, and
rotations.
Bitwise Operators
1. AND (&)
Example:
A = 0b1101 # 13 in decimal
B = 0b1011 # 11 in decimal
Use Case:
if num & 1 == 0:
⸻
2. OR (|)
Example:
A = 0b1101
B = 0b1011
Use Case:
3. XOR (^)
Example:
A = 0b1101
B = 0b1011
Use Case:
x=5
y=7
x=x^y
y=x^y
x=x^y
print(x, y) # Output: 7, 5
4. NOT (~)
Example:
A = 0b00001111 # 15 in decimal
Use Case:
Example:
A = 5 # 0b0101
Use Case:
Example:
A = 20 # 0b10100
Use Case:
Practical Applications
num = 7
if num & 1:
print("Odd")
else:
print("Even")
num = 0b1010 # 10
READ = 0b001
WRITE = 0b010
EXECUTE = 0b100
num = 8
Conclusion
Bit manipulation is a fundamental concept in computer science used for efficient computation. It is
widely applied in optimization, cryptography, graphics, and low-level programming. Understanding
bitwise operations helps in designing efficient algorithms and solving problems at the hardware
level.