Boolean operators precedence

46,538

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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))

Solution 5

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
Share:
46,538
Mika
Author by

Mika

Updated on July 09, 2022

Comments

  • Mika
    Mika almost 2 years

    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.

  • Mika
    Mika over 11 years
    Alright. I had boolean operators in mind when posting. Thanks for reminding me of bitwise operators.
  • mbomb007
    mbomb007 about 8 years
    According to this, C's precedence is actualy NOT > AND > XOR > OR, with AND coming before XOR. JavaScript and Java also follow this order.
  • Junior
    Junior about 7 years
    Is there a good reason to have different precedence? I think that (in an ideal world) boolean operators should have the same level of precedence and logical operations to be always performed from left to right. Is there a good reason to be in another way? true xor false or true should be evaluated as true ((true xor false) or true), not as false (true xor (false or true))
  • FERcsI
    FERcsI about 7 years
    Yes. The reason is very similar to arithmetic operators. This is a "different algebra" (boolean algebra). AND is a kind of multiplication, and OR is like addition. There are areas, where it is more important than standard algebra (e.g. construction of digital circuits).
  • ndrwnaguib
    ndrwnaguib over 5 years
    @mbomb007 It's the Bitwise AND not the Logical AND
  • mbomb007
    mbomb007 over 5 years
    @AndrewNaguib Read the link I posted. Bitwise AND comes before Bitwise XOR in C.
  • ndrwnaguib
    ndrwnaguib over 5 years
    Yes, he doesn’t mean the bitwise AND, he is mentioning the logical AND.
  • Petruza
    Petruza about 5 years
    C/C++ doesn't have a logical XOR, only a bitwise XOR. An actually no language that I know of has a logical XOR. XOR makes a lot of sense when doing bitwise operations, but with boolean operations you can resolve it using combinations of AND and OR, or other higher level language constructs.
  • Thaina Yu
    Thaina Yu about 4 years
    @Petruza C# allow ^ to reuse on boolean operator because it has specific boolean type. And it make somehow a compact logic albeit unfamiliar for the eye
  • jaro2gw
    jaro2gw about 4 years
    The first 3 asserts here are totally redundant and prove nothing. If the precedence of the && and || were opposite, the expressions on the left side of == still evaluate to the exact same values
  • Jesse Chisholm
    Jesse Chisholm over 2 years
    And if you don't feel right using ^ as a logical XOR you can use != which will evaluate the same as XOR.
  • Jesse Chisholm
    Jesse Chisholm over 2 years
    You can also abbreviate boolean XOR as A != B. But then you get into the precedence ordering of comparison operators vs. boolean operators. So be careful.