Bitwise Operations Explained
Learn about AND, OR, XOR, NOT, and shift operations in hexadecimal and binary.
Bitwise Operations Explained
Bitwise operations are fundamental to low-level programming and digital electronics. This guide explains the main bitwise operations and their applications.
What Are Bitwise Operations?
Bitwise operations work on individual bits of binary numbers. They’re essential for:
- Hardware programming
- Cryptography
- Data compression
- Network protocols
AND Operation (&)
The AND operation compares each bit of two numbers. The result is 1 only if both bits are 1:
1010 (10 in decimal)
& 1100 (12 in decimal)
------
1000 (8 in decimal)
OR Operation (|)
The OR operation compares each bit of two numbers. The result is 1 if either bit is 1:
1010 (10 in decimal)
| 1100 (12 in decimal)
------
1110 (14 in decimal)
XOR Operation (^)
The XOR (exclusive OR) operation compares each bit. The result is 1 if the bits are different:
1010 (10 in decimal)
^ 1100 (12 in decimal)
------
0110 (6 in decimal)
NOT Operation (~)
The NOT operation inverts all bits:
~ 1010 (10 in decimal)
------
0101 (5 in decimal, using 4-bit representation)
Shift Operations
Left Shift (<<)
Shifts all bits to the left by the specified number of positions:
1010 << 2 = 101000 (10 << 2 = 40)
Right Shift (>>)
Shifts all bits to the right by the specified number of positions:
1010 >> 2 = 10 (10 >> 2 = 2)
Practical Applications
- Flag Manipulation: Setting, clearing, and toggling specific bits
- Permissions Systems: Using bitmasks for access control
- Data Packing: Storing multiple values in a single byte
- Performance Optimization: Replacing multiplication/division with shifts
Conclusion
Mastering bitwise operations gives you powerful tools for low-level programming and optimization. Practice with our hex calculator to see these operations in action!