Mika
Mika

Reputation: 1245

Boolean operators precedence

I would like to know if operator precedence in programming languages depends on implementation or there is a fixed rule that all languages follow. And if possible, could you order the following operators with highest precedence first: AND, OR, NOT, XOR.

Upvotes: 30

Views: 68644

Answers (6)

user21508463
user21508463

Reputation:

In traditional Boolean algebra, the precedence is not > and > or, if you write the not as an overbar. and is "multiplicative", or is "additive". The case of not as an operator is less clear. IMO, xor should be assigned the same priority as the inclusive or.

  • In C/C++, ! > != > && > || (there is no logical ^, != acts as a xor)
  • In C#, ! > & > ^ > |
  • In Visual Basic, Not > And > Or > Xor
  • In FORTRAN, .not. > /= > .and. > .or. (there is no .xor., /= acts as a xor)
  • In Python, != > not > and > or (there is no xor, != acts as xor)
  • In Pascal, not > and > or > <> (there is no xor, <> acts as xor)

Upvotes: 1

Joseph Tesfaye
Joseph Tesfaye

Reputation: 874

You have seen that when expressions mix && with || that evaluation must be done in the correct order. Parentheses can be used to group operands with their correct operator, just like in arithmetic. Also like arithmetic operators, logical operators have precedence that determines how things are grouped in the absence of parentheses.

In an expression, the operator with the highest precedence is grouped with its operand(s) first, then the next highest operator will be grouped with its operands, and so on. If there are several logical operators of the same precedence, they will be examined left to right.

It is common for programmers to use parentheses to group operands together for readability even when operator precedence alone would work.

enter image description here

Source

Upvotes: 20

daubmannus
daubmannus

Reputation: 518

Obvious and easy way to prove that AND is of higher precedence than OR is to compare results of statements with and without parentheses:

std::cout << std::boolalpha;
std::cout << ( true || false && false )     << std::endl;   // gives true
std::cout << ( ( true || false ) && false ) << std::endl;   // gives false

Upvotes: 1

FERcsI
FERcsI

Reputation: 410

There are three basic Boolean operators: NOT, AND, OR. XOR is just a simple version of A AND NOT B OR NOT A AND B or (A OR NOT B) AND (NOT A OR B). So, only these three have common precedence: NOT > AND > OR. XOR has different position in languages, but it has surely not higher precedence than AND and not lower than OR. Most languages (e.g. C/C++/Javascript etc.) have it between AND and OR, but in other ones (e.g. Perl) XOR has the same precedence as OR.

(OR can be expressed using only AND and NOT, but it is still a basic operator: A OR B = NOT(NOT A AND NOT B))

Upvotes: 4

Stephan Eggermont
Stephan Eggermont

Reputation: 15907

Boolean or bitwise? There is no fixed rule, most languages have similar rules but differ in details. Look it up in the language definition.

Upvotes: 4

emiljho
emiljho

Reputation: 572

I googled and found out this which says that some languages like APL and SmallTalk do not have operator precedence rules and they strictly evaluate expressions from left to right/left to right.

However,relative order of precedence followed is NOT > XOR > AND > OR in most of the languages especially those derived from C

Upvotes: 24

Related Questions