DiGrande.it

Blind, Braille and Embossing Technologies

This site uses cookies to personalize content and ads, provide social media features and analyze links. By closing this banner or continuing to browse, you consent to their use.
Read the DiGrande.it Cookie Policy

Logical operators

A logical operator is a word that connects two numeric or Boolean expressions and generates a value. Depending on the type of operands connected, we define them as conditional operators when we perform operations on Boolean types, while we call them logical operators when we operate on integers. Three binary operators and one single operator are available.

- AND - Conditional or logical connection (binary operator);

- OR - Conditional or logical disconnection (binary operator);

- XOR - Exclusive conditional or logical disconnection (binary operator);

- NOT - Conditional or Logical Denial (Unary Operator).

Conditional or binary logic operators need to compare two operands, while the unary operator operates only on one operand. Conditional operators operate on Boolean type operands. Logic operators, on the other hand, operate only on integers, so when there are floating point numbers, the numbers under a logic operator are automatically converted into unsigned integers. Integers always use 64 bits. To understand the logical operators it is necessary to deepen the Boolean algebra and the binary numerical system.

The AND operator performs a logical conjunction, returning as TRUE result if both operands are valid TRUE, otherwise FALSE. Since it is a binary operator, and the individual bits of each operand have two values, there are four possible combinations.

- TRUE AND TRUE = TRUE

- FALSE AND FALSE = FALSE

- FALSE AND TRUE = FALSE

- TRUE AND FALSE = FALSE

For example:

- C = 40 AND 8

Variable C is assigned the resulting value 8, since the 3rd bit in the number 40 is TRUE. See the numeric binary system.

The OR operator performs a logical disconnection, returning as TRUE result if one of the operands is TRUE, otherwise FALSE if both operands are FALSE. Since it is a binary operator, and the individual bits of each operand have two values, there are four possible combinations.

- TRUE OR TRUE = TRUE

- FALSE OR FALSE = FALSE

- FALSE OR TRUE = TRUE

- TRUE OR FALSE = TRUE

For example:

- C = 32 OR 8

The resulting value 40 is assigned to variable C, since the sum of the bits of the two operands returns the value 40. See the numeric binary system.

- C = 40 OR 9

Variable C is assigned the value 41. This shows that OR logic decoupling cannot be superimposed on an arithmetic sum.

Operator XOR performs an exclusive disconnection, returning as TRUE result only if the operands are different from each other. Since it is a binary operator, and the individual bits of each operand have two values, there are four possible combinations.

- TRUE XOR TRUE = FALSE

- FALSE XOR FALSE = FALSE

- FALSE XOR TRUE = TRUE

- TRUE XOR FALSE = TRUE

For example

- C = 40 XOR 8

The resulting value 32 is assigned to variable C, since the exclusive separation of the bits of the two operands returns the value 32. See the numeric binary system.

- C = 40 XOR 63

Variable C is assigned the value 23, result of the exclusive disjunction of the bits of 40 and 63.

The operator NOT is used to execute the logical negation, returning as a result the value opposite the incoming one. This is a unary operator, meaning it only needs one operand to be applied. If an expression returns to TRUE, then "NOT expression" returns to FALSE value, and vice versa. Applying this operator to numeric variables will automatically convert signed numbers to unsigned numbers.