Bitwise Operators in C Programming (Complete Guide with Examples)
This tutorial explains bitwise operators in C, which allow performing operations directly on binary representations of integers. It covers AND, OR, XOR, NOT, left shift, and right shift operators with examples to manipulate bits efficiently.
1. What are Bitwise Operators
- Bitwise operators operate at the binary level on integer operands.
- Useful for low-level programming, embedded systems, and performance-critical applications.
2. List of Bitwise Operators
| OperatorSymbolDescription | ||
| AND | & | Sets each bit to 1 if both bits are 1 |
| OR | ` | ` |
| XOR | ^ | Sets each bit to 1 if bits are different |
| NOT | ~ | Inverts all bits |
| Left Shift | << | Shifts bits to left, fills 0 on right |
| Right Shift | >> | Shifts bits to right, fills 0 on left (logical) |
3. Example: Bitwise AND, OR, XOR
Output:
4. Example: Left Shift and Right Shift
Output:
Explanation:
<<shifts bits left (multiplies by 2)>>shifts bits right (divides by 2)
5. Key Points to Remember
- Bitwise operators work only on integer types
- Useful for flags, masks, and efficient calculations
&,|,^operate bit by bit~inverts bits,<<and>>shift bits- Essential in low-level programming, embedded systems, and networking