1. 运算法优先级
| Precedence | Operator | Description | Associativity |
| 1 highest | :: | Scope resolution (C++ only) | Left-to-right |
| 2 | ++ | Suffix increment | |
| -- | Suffix decrement | ||
| () | Function call | ||
| [] | Array subscripting | ||
| . | Element selection by reference | ||
| -> | Element selection through pointer | ||
| typeid() | Run-time type information(C++ only) (see typeid) | ||
| const_cast | Type cast (C++ only) (see const cast) | ||
| dynamic_cast | Type cast (C++ only) (see dynamic_cast) | ||
| reinterpret_cast | Type cast (C++ only) (see reinterpret cast) | ||
| static_cast | Type cast (C++ only) (see static cast) | ||
| 3 | ++ | Prefix increment | Right-to-left |
| -- | Prefix decrement | ||
| + | Unary plus | ||
| - | Unary minus | ||
| ! | Logical NOT | ||
| ~ | Bitwise NOT | ||
| (type) | Type cast | ||
| * | Indirection (dereference) | ||
| & | Address-of | ||
| sizeof | |||
| new, new[] | Dynamic memory allocation (C++ only) | ||
| delete, delete[] | Dynamic memory deallocation (C++ only) | ||
| 4 | .* | Pointer to member (C++ only) | Left-to-right |
| ->* | Pointer to member (C++ only) | ||
| 5 | * | Multiplication | |
| / | Division | ||
| % | Modulo(remainder) | ||
| 6 | + | Addition | |
| - | Subtraction | ||
| 7 | << | Bitwiseleft shift | |
| >> | Bitwiseright shift | ||
| 8 | < | Less than | |
| <= | Less than or equal to | ||
| > | Greater than | ||
| >= | Greater than or equal to | ||
| 9 | == | Equal to | |
| != | Not equal to | ||
| 10 | & | Bitwise AND | |
| 11 | ^ | Bitwise XOR (exclusive or) | |
| 12 | | | Bitwise OR (inclusive or) | |
| 13 | && | Logical AND | |
| 14 | || | Logical OR | |
| 15 | ?: | Right-to-left | |
| 16 | = | Direct assignment | |
| += | Assignment by sum | ||
| -= | Assignment by difference | ||
| *= | Assignment by product | ||
| /= | Assignment by quotient | ||
| %= | Assignment by remainder | ||
| <<= | Assignment by bitwise left shift | ||
| >>= | Assignment by bitwise right shift | ||
| &= | Assignment by bitwise AND | ||
| ^= | Assignment by bitwise XOR | ||
| |= | Assignment by bitwise OR | ||
| 17 | throw | Throw operator (exceptions throwing, C++ only) | |
| 18 | , | Left-to-right |
2. 左移、右移运算法
位移运算符分为算术位移(Arithmetic Shift)和逻辑位移(Logic Shift):算术位移时,移出的位被丢弃,移进的位在左移(Left Shift)时补0;右移(Right Shift)时补符号位,保证符号位不变。逻辑位移时,移出的位被丢弃,移进的位不论左移和右移都补0。另外,当移动的位数超过类型的长度时,将会对位数取余数,按余数进行移位。
如:10100110 >>5(假设字长为8位),则得到的是 11111101
10100110 >>13,得到的也是11111101。
本文详细介绍了C++中运算符的优先级及其结合性,并重点讲解了左移与右移运算符的功能及使用场景。通过具体实例说明了位移运算在不同情况下的行为。
819

被折叠的 条评论
为什么被折叠?



