Summary
1) Bit operator in C language:
Operator | significance | rule |
---|---|---|
& | Bitwise and | All 1s get 1, and 0s get 0 |
I | Bitwise or | 1 is 1 and all 0s are 0 |
^ | Bitwise exclusive or | Same as 0, different as 1 |
~ | Negate | 1 becomes 0, 0 becomes 1 |
<< | Shift left | High bits are discarded, low bits are filled with 0 |
·>> | Shift right | High bit complement sign bit, low bit discarded |
2) Points to note when moving left and right:
left operand must be of integer type
- Char and short are implicitly converted
and then operated
- Char and short are implicitly converted
right operand must be: [0,31],
otherwise the result is undefined (different compilers process different results)
Left shift operator << shifts the binary bits of the
equivalent to multiplying by the n power of 2,
is more efficient
right shift operator>> shifts the binary bit of the
equivalent to dividing by the n power of 2,
is more efficient
- Avoid mixed operations such as bitwise operators, logical operators, and mathematical operators. If this is necessary, use parentheses to indicate the order of calculation. (
single calculation shift ratio, according to logic three assignments)
3) Points to note in bit operation:
- There is no short-circuit rule for bit operation,
will participate in the operation
result of the bit operation is an integer, not 0 or 1
- The priority of bit operation is higher than logic operation (
single arithmetic shift ratio is assigned by logic three)
4) Exchange the values of two integer variables:
Demo1:
part and method: do not use intermediate variables, but there is an overflow problem
#define SWAP(a, b) \ { \ a = a + b; \ b = a - b; \ a = a - b; \ }
Demo2:
bit operation method: XOR (
two identical values XOR result is 0, any number and 0 XOR is still itself)
#define SWAP(a, b) \ { \ a = a ^ b; \ b = a ^ b; \ a = a ^ b; \ }
Anatomy of bitwise operators
The C language was originally designed to develop the UNIX operating system. The operating system runs on a hardware platform, which inevitably involves bit operations, and requires operations on the bits (0 and 1) of the hardware. The bit operation in C language directly maps to the bit operation in the hardware system.
The bit operator directly operates on the bit bit, and its
is the most efficient.
1. Bitwise operators in C language
Operator | significance | rule |
---|---|---|
& | Bitwise and | All 1s get 1, and 0s get 0 |
I | Bitwise or | 1 is 1 and all 0s are 0 |
^ | Bitwise exclusive or | Same as 0, different as 1 |
~ | Negate | 1 becomes 0, 0 becomes 1 |
<< | Shift left | High bits are discarded, low bits are filled with 0 |
·>> | Shift right | High bit complement sign bit, low bit discarded |
2. Points to note about shifting left and right
left operand must be of integer type
- char and short are implicitly converted to int
- char and short are implicitly converted to int
right operand of 16167e38ecbc47 must be: [0,31],
otherwise the result is undefined (different compilers handle different results)
Left shift operator << shifts the binary bits of the
equivalent to multiplying by the n power of 2,
is more efficient
right shift operator>> shifts the binary bit of the
equivalent to dividing by the n power of 2, which is more efficient
- Demo1
d << 2; // error, 左操作数必须是整型 int x = 10 >> 1; // ok,"右移等效除以2的n次方",输出5 int y = -1 << 1; // ok,"左移等效乘以2的n次方",输出-2 int z = 3 << -1; // -1不在[0,31]范围中,结果未定义,不同编译器不同处理 // 编译器亦提示了错误:Possible overflow in shift operation // gcc 1, 左移-1相当于右移1,那就除以2,得到了1 // bcc -2147483648, int的最小值 // vc 0, 本来就未定义,给你0把
Avoid mixed operations such as bitwise operators, logical operators, and mathematical operators. If this is necessary, use parentheses to indicate the order of calculation. (
single calculation shift ratio, according to logic three assignments)
- Demo2
0x1 << 2 + 3 输出多少? 等价于 0x1 << (2 + 3),输出为32 // 如果这行代码作者本意是先左移,再加法,实际执行结果就不是期望的 // 所以在写代码时要尽量用括号()来表示清楚计算次序
3. Points of Attention in Bit Operation
- There is no short-circuit rule for bit operations,
will participate in the operation
- The result of bit operation
is an integer, not 0 or 1
The priority of bit operation is higher than logic operation (
single arithmetic shift ratio is assigned by logic three)
int i = 0; int j = 0; int k = 0; if(++i | ++j & ++k) // 所有的运算数都会执行到,因此i j k判断后都为1 printf("cc");
4. Simple example of bit operation application
Exchange the values of two integer variables
Demo1:
part and method: do not use intermediate variables, but there is an overflow problem
#define SWAP(a, b) \ { \ a = a + b; \ b = a - b; \ a = a - b; \ }
Demo2:
-bit arithmetic mode: use XOR (
two identical values XOR result is 0, any number and 0 XOR is still itself)
#define SWAP(a, b) \ { \ a = a ^ b; \ b = a ^ b; \ a = a ^ b; \ }
This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。