Logical operator

from Wikipedia, the free encyclopedia

A logical operator is a function that delivers a truth value . With two-valued, Boolean logic , it delivers true or false; with multi-valued logic , other values ​​can be delivered accordingly. Logical operators can have any number of operands .

A typical example of a Boolean logical operator is the AND operation : It always returns true if all operands are also true.

The logical operators are of particular importance in practical application in programming languages and in digital circuits . In programming languages, with the exception of the “NOT” operator with only one operand, usually only operators with two operands are available. In addition to the Boolean operators, comparison operators are also part of the standard repertoire of high-level programming languages. In circuit technology , on the other hand, “AND” / “OR” links with several operands are common, which are implemented using logic gates or series or parallel connection of switches or relays .

Logical operators in programming languages

A more complex logical expression is created by logically linking (mostly two) logical expressions. Depending on the type of link operator, the compound expression is true if, for example, both linked expressions are true ( AND link ) or if at least one of the linked expressions is true ( OR link ). The logical operators of the programming languages ​​may differ from the joiners in propositional logic in the order of evaluation or in side effects that have to be taken into account .

With regard to the type, number and spelling of the logical operators, the individual programming languages differ greatly from one another (see table). There are also differences with regard to the internal representation of the truth values: In most programming languages ​​the truth value is incorrectly represented as the number 0. The truth value true is often presented as the number 1, but in many cases also each is different from 0 value as a true interpretation. A notable exception are many command interpreters , including the various Unix shells , in which the success of an executed command is indicated by the return value 0, the failure by a value other than 0, which shows the type of error that has occurred. As a generalization of this, in the logical expressions of this command interpreter the value 0 (“no error”) is interpreted as true and any value other than 0 (“error”) is interpreted as false.

Comparison operators compare two values, such as numerical values, and deliver a truth value depending on the outcome of the comparison (larger, smaller, etc.).

Examples of logical operators

operator mathematics python Fortran Delphi, Pascal Visual Basic C, C ++, C #, Java, PHP Pearl Batch
Comparisons greater > > .GT. > > > >, gt GTR
smaller < < .LT. < < < <, according to LSS
greater than or equal to > = .GE. > = > = > = > =, ge GEQ
Smaller or equal <= .LE. <= <= <= <=, le LEQ
equal ==, is .EQ. = = == ==, eq EQU, ==
unequal <> 1) ,! =, Is not .NE. <> <> ! = ! =, no NEQ
connections And ( conjunction ) &, and, all () .AND. AND And, AndAlso 2) &, && &&, &, and n / A.
Or ( disjunction ) |, or, any () .OR. OR Or, OrElse 2) |, || ||, |, ^, or n / A.
Not ( negation ) not .NOT. NOT Need ~ ,! !, ~, not NOT
1) in Python 2, now obsolete
2) New in .NET versions

Examples of logical expressions

    IF a > b THEN

In this case, it is evaluated whether a is greater than b. If the statement is true, the statements that are introduced with THEN are carried out. Otherwise the instructions will be skipped.

    IF i AND j THEN

In this case it is evaluated whether i is true and j is true . If i and j are true, the instructions that are introduced with THEN are carried out. If one of the values ​​is incorrect (or both), the instructions are skipped.

Evaluation order, commutativity and side effects

The logical operators of a programming language are not an exact equivalent of the connectives in propositional logic , because no in the evaluation of connectives side effects may occur. In a programming language, this may very well be the case if, for example, B. functions with side effects can be called within the logical expression. In this case, the order of evaluation plays a role; H. the law of commutativity (e.g. for the conjunction : a ∧ b = b ∧ a) does not apply if a or b not only have a value but also an effect . For this reason, most programming languages ​​define a fixed order of evaluation for logical expressions.

If you evaluate a conjunction a ∧ b from left to right and find that a is already wrong, you already know at this point and without having to take a closer look at b that the whole conjunction will be wrong. This knowledge can be used to optimize the running time by completely dispensing with the evaluation of b in this case ( lazy evaluation ) - at the price, however, that a possible effect of b is then absent. Because of this, uncritical expression optimization is a problem. There are two ways of dealing with this: Expressions or parts of expressions with side effects can be excluded from the optimization, i. H. always be evaluated; or the premature termination of the evaluation of logical expressions can be included exactly in the language specification so that the programmer can at least accurately predict which side effects will occur in which situation and which will not.

The latter is the case in programming languages ​​such as C , C ++ or Java : Here it is specified that when evaluating a conjunction a ∧ b, in the notation of these languages ​​a && b, the evaluation of b is omitted if a is already false ; and that when evaluating the disjunction a ∨ b, in the notation of these languages ​​a || b, the evaluation of b is omitted if a is already true . This does not represent a fundamental difference to the respective propositional joiners if the sub-expressions to be evaluated have no effect.

Other programming languages ​​like Ada (or C # ) offer conjunctions and disjunctions in the propositional sense (“AND” and “OR”) as well as operators like “&&” or “||” in C, in Ada “AND THEN” or . Called "OR ELSE".

Example of a logical operator in multi-valued logic

The following example from electrical engineering corresponds to a logical operator with 10 operands in a 4-value logic:

10 devices are connected to a bus , each of which can deliver the truth values ​​“high” (5 volts), “low” (0 volts) and “Z” (high resistance). The state of the bus can be described by the following logical operator:

 O (Gerät1, Gerät2, …, Gerät10):
   „Kurzschluss“ … wenn mindestens ein Gerät „high“ liefert und mindestens ein Gerät „low“
   „high“        … wenn mindestens ein Gerät „high“ liefert und kein Gerät „low“
   „low“         … wenn mindestens ein Gerät „low“ liefert und kein Gerät „high“
   „hochohmig“   … wenn alle Geräte „hochohmig“ liefern

See also