Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Operator Precedence in Cairo1.0 #3552

yuvalsw started this conversation in Polls
Discussion options

In many popular languages + and - have higher precedence than the bitwise operators (<<, >>, &, ^, | ). So for example 0x1234 & 0x1100 + 0x5678 & 0x0011 is interpreted as 0x1234 & (0x1100 + 0x5678) & 0x0011 and not as (0x1234 & 0x1100) + (0x5678 & 0x0011).

Seems like this started in B/C from using & as 'logical and', then kept this way when && was added, to simplify migration. The relative precedence between == and & was "fixed" in some languages (e.g. Rust and Python), but + still has higher precedence than &.

A few examples of precedence in other languages (taking representatives of the operators):
Rust/Python: + & == &&
C/C++/C#/Java: + == & &&

We think the correct precedence is: & + == &&.
The disadvantage is that if expressions are copied from other languages like Rust/Python, they might be interpreted differently (note that this is anyway the case with == and & in some other languages, like C/C++/C#/Java).

Another note: whenever the expression is too ambiguous for the writer/reader, it is anyway recommended to add parentheses to increase readability.

What should be the relative precedence of `+` and `&`?
`&` has higher precedence: a & b + c == (a & b) + c.
75%
`+` has higher precedence: a & b + c == a & (b + c).
25%

16 votes

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
1 participant

AltStyle によって変換されたページ (->オリジナル) /