Logical OR vs Logical AND: what should be more mandatory?

I am writing a small parser that will have an OR operator and an AND operator. When do you see a series of OR and AND that you expect to be more attached? Given the expression a & b | c, do you expect it to mean (a&b)|cor a&(b|c)? Can you give any reason to prefer each other?

+3
source share
4 answers

Do what everyone else does; And it binds more tightly than OR (see, for example, C Operator Priority Table ). This is an agreement that everyone is waiting for, so accept the principle of least surprise .

. , ; ., , http://en.wikipedia.org/wiki/Boolean_logic#Other_notations.

, , . !

+3

AND OR * - , , AND , OR, , * +:

A B A*B A&B   A+B   A|B
0 0   0   0   0       0
0 1   0   0   1       1
1 0   0   0   1       1
1 1   1   1   1(>0)   1
+2

, , , PEMDAS , . .

I recommend that you recommend users use parentheses wherever there is ambiguity.

0
source

Usually and takes precedence over | in many scenarios. But you can limit the expressions as a full bracket.

0
source

Source: https://habr.com/ru/post/1784833/


All Articles