Logic

This page was translated by a robot.

Logical links form the basic building blocks of logic circuits used in modern processors. Here, binary input signals are converted into binary output signals on the basis of fixed rules. In programming, binary values ​​are used to store logical values ​​and are called boolean values.

Boolean values ​​are represented in C with the numbers 0 and 1. In C++, the type was also boolintroduced with the keywords falseand true. C and C++ use the following operators to concatenate binary values:

The operators of the negation NOT expect an operand and reverse the respective truth value, 0 becomes 1 and vice versa. The other operators expect two operands and combine them according to the following truth table:

    | EQU |  OR | AND | XOR
----+-----+-----+-----+-----
0 0 |  1  |  0  |  0  |  0
0 1 |  0  |  1  |  0  |  1
1 0 |  0  |  1  |  0  |  1
1 1 |  1  |  1  |  1  |  0

Details

In the C and C++ languages, a distinction is made between logical and bitwise operators in binary operations. The logical operators each interpret the given operands as a boolean value. The bitwise operators, on the other hand, treat each bit of the operand as a Boolean value and carry out the link for each bit. In the case of operations with two operands, all bits are linked in pairs with the bitwise operators. Examples are listed with the respective operators.

It should be noted that the logical XOR operator does not exist in principle. However, since combining two Boolean values ​​with the not equal operator!= has the same effect, it is listed on this page as an XOR operator for the sake of completeness.

When converting a non-boolean value to a boolean, any value that evaluates to is assumed to 0be falseand any value that does not 0evaluate to is trueassumed to be.

The logical operators are mainly used for conditions, the bitwise operators mainly for masks.

Conditions

Control structures usually need a boolean value to execute a certain part of the program or not, as the case may be. The expression that leads to the decisive Boolean value is called a condition and is always in round brackets ( )after the corresponding control structure keyword.

A condition is written and treated like a normal statement. However, the trailing semicolon ;is omitted within the round brackets. Only with the forloop; does the condition come second within the round brackets () , separated by semicolons.

A condition is always evaluated as a boolean value. Accordingly, the logical operators NOT! , AND&& and OR|| are very common in conditions. Equally, however, comparison operators such as Less< than and Greater> than return a Boolean value and are generally used in numerical comparisons for conditions.

If a pointer type is specified as a condition, then this is evaluated as a Boolean value according to the above rules. This means that all pointers are trueevaluated unless they are null pointers . Caution: Neither the C or C++ languages ​​themselves, nor any compiler, automatically initialize pointers to zero; this must always be done manually by the programmer.

Those switching from other languages ​​to C or C++ should also be warned at this point that the equals operator== in C ==has two equals signs and not one. The operator with an equal sign =is the assignment operator .

Masks

The bitwise NOT~ , AND& , OR| , and XOR^ operators and their corresponding assignment operators are used primarily for low-level programming, such as systems programming. Since they change individual bits, dealing with these operators is also often referred to as bit fiddling (bit fiddling, also to be understood as bit tinkering, or in good Swiss German: Umegfätterle with Bittli).

However, these operators are also used in normal everyday programming, especially to edit so-called masks . Masks are settings stored as a sequence of bits, with each bit representing a setting. The individual bits are also referred to as flags (flags, markings). The following is an example of common mask operations:


0001
0010
0100
1000  



Initializing with flags, m = 0011
Setting a flag,          m = 0111
Switching a flag,        m = 1111
Testing a flag               1111
deleting a flag,         m = 1110
enum mask{
  VALID     = 0x01,
  RUNNING   = 0x02,
  COMPUTING = 0x04,
  ERROR     = 0x08,
};

int main(){
  enum mask m = VALID | RUNNING;
  m |= COMPUTING;
  m ^= ERROR;
  if(m & ERROR) {
    m &= ~VALID;
  }
  return 0;
}
Next Chapter: Number Systems