C vs. Python Operator Precedence: Beware of (Bitwise) Logical Op.

51 篇文章 2 订阅
24 篇文章 0 订阅

Conclusion

the most important difference between C and Python operator difference is that between comparison operators and (bitwise) logical operators

C

C++ Operator Precedence - cppreference.com

pay attention to row 8~10, the comparison operators, which have higher precedence than bitwise then logical operators.

PrecedenceOperatorDescriptionAssociativity
1::Scope resolutionLeft-to-right ➔
2a++   a--Suffix/postfix increment and decrement
type()   type{}Functional cast
a()Function call
a[]Subscript
.   ->Member access
3++a   --aPrefix increment and decrementRight-to-left ←
+a   -aUnary plus and minus
!   ~Logical NOT and bitwise NOT
(type)C-style cast
*aIndirection (dereference)
&aAddress-of
sizeofSize-of[note 1]
co_awaitawait-expression (C++20)
new   new[]Dynamic memory allocation
delete   delete[]Dynamic memory deallocation
4.*   ->*Pointer-to-memberLeft-to-right ➔
5a*b   a/b   a%bMultiplication, division, and remainder
6a+b   a-bAddition and subtraction
7<<   >>Bitwise left shift and right shift
8<=>Three-way comparison operator (since C++20)
9<   <=   >   >=For relational operators < and ≤ and > and ≥ respectively
10==   !=For equality operators = and ≠ respectively
11a&bBitwise AND
12^Bitwise XOR (exclusive or)
13|Bitwise OR (inclusive or)
14&&Logical AND
15||Logical OR
16a?b:cTernary conditional[note 2]Right-to-left ←
throwthrow operator
co_yieldyield-expression (C++20)
=Direct assignment (provided by default for C++ classes)
+=   -=Compound assignment by sum and difference
*=   /=   %=Compound assignment by product, quotient, and remainder
<<=   >>=Compound assignment by bitwise left shift and right shift
&=   ^=   |=Compound assignment by bitwise AND, XOR, and OR
17,CommaLeft-to-right ➔

Python

6.10. Comparisons

Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics:

Precedence and Associativity of Operators in Python

it should be noted that for python bitwise operator has higher precedence than comparison op. then logical op.

The operator precedence in Python is listed in the following table. It is in descending order (upper group has higher precedence than the lower ones).

OperatorsMeaning
()Parentheses
**Exponent
+x, -x, ~xUnary plus, Unary minus, Bitwise NOT
*, /, //, %Multiplication, Division, Floor division, Modulus
+, -Addition, Subtraction
<<, >>Bitwise shift operators
&Bitwise AND
^Bitwise XOR
|Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not inComparisons, Identity, Membership operators
notLogical NOT
andLogical AND
orLogical OR
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值