“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute each day to remember the basics of C language.
“C Language Beginner’s Essential Knowledge Notes Series of 100 Articles”“
21. Bitwise Operators, Summary of Common Usages and Techniques
1. Basic Syntax of Bitwise Operators
Bitwise operators directly manipulate the binary bits of integers and are applicable to integer types (char, int, long, etc.):
| Operator | Name | Example | Description |
|---|---|---|---|
| & | Bitwise AND | a & b | Result is 1 when both corresponding bits are 1 |
| | | Bitwise OR | a | b | Result is 1 when at least one corresponding bit is 1 |
| ^ | Bitwise XOR | a ^ b | Result is 1 when corresponding bits are different |
| ~ | Bitwise NOT | ~a | All bits are inverted (0 becomes 1, 1 becomes 0) |
| << | Left Shift | a << n | All bits are shifted left by n positions |
| >> | Right Shift | a >> n | All bits are shifted right by n positions |
2. Bitwise Operation Code Examples
1. Basic Bit Operations (8-bit Character Integer)
// Binary representation of character or integer
unsigned char a = 0b10101010; // 170
unsigned char b = 0b11001100; // 204
printf("a & b = %d\n", a & b); // 0b10001000 (136)
printf("a | b = %d\n", a | b); // 0b11101110 (238)
printf("a ^ b = %d\n", a ^ b); // 0b01100110 (102)
printf("~a = %d\n", (unsigned char)~a); // 0b01010101 (85)
2. Shift Operations
int x = 0b00001111; // 15
printf("x << 2 = %d\n", x << 2); // 0b00111100 (60)
printf("x >> 1 = %d\n", x >> 1); // 0b00000111 (7)
3. Special Properties of Bitwise Operations
1. Properties of XOR
a ^ a = 0 // Same results in 0
a ^ 0 = a // XOR with 0 remains unchanged
a ^ b ^ b = a // Two XORs restore
2. Mathematical Significance of Shift Operations
// Left shift n positions is equivalent to multiplying by 2ⁿ
x << n is equivalent to x * 2ⁿ
// Right shift n positions is equivalent to dividing by 2ⁿ (rounding down)
x >> n is equivalent to x / 2ⁿ (rounding down)
4. Practical Tips for Bitwise Operations
1. Determine Odd or Even
if (x & 1) {
// Odd (least significant bit is 1)
} else {
// Even (least significant bit is 0)
}
2. Swap Two Numbers (Without Temporary Variable)
a ^= b;
b ^= a;
a ^= b;
5. Compound Assignment Operators
| Operator | Equivalent Form |
|---|---|
| a &= b | a = a & b |
| a |= b | a = a | b |
| a ^= b | a = a ^ b |
| a <<= n | a = a << n |
| a >>= n | a = a >> n |
6. Common Mistakes
1. Sign Bit Issues
int x = -1;
x >> 1; // Arithmetic right shift for signed numbers (keeps sign bit), result is still -1
The above is arithmetic right shift, below is logical right shift for unsigned numbers.
unsigned int y = (unsigned int)x >> 1; // Logical right shift
2. Shift Overflow
int a = 1;
a << 32; // Undefined behavior (when n >= type bit count)
Correction:
if (n < sizeof(a)*8) {
a <<= n;
}
7. Practical Application Cases
1. Bitmask Operations
#define READ_PERM 0x1 // 0001
#define WRITE_PERM 0x2 // 0010
#define EXEC_PERM 0x4 // 0100
unsigned char perm = 0;
perm |= READ_PERM | WRITE_PERM; // Set read and write permissions
if (perm & READ_PERM) { // Check read permission
printf("Readable\n");
}
2. Color Processing (RGB)
unsigned int color = 0x00FF00; // Green
int red = (color >> 16) & 0xFF;
int green = (color >> 8) & 0xFF;
int blue = color & 0xFF;
3. Bit Field Structures
Bit Field Structure:
struct {
unsigned int flag1 : 1;
unsigned int flag2 : 1;
} status;
Some students have contacted me, wanting to create a study and exchange group. Previously, I hesitated to create a group due to concerns about advertisements, but I think having a group would indeed be convenient, so I will create one to try it out.
If you need it, hurry up and join; the validity period is 7 days.

———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author, and some content and images are sourced from the internet. Please feel free to consume them; the views are for learning reference only~~]


“If you like C, please like it”
Click the bottom right corner to view
“