# Logical Operators

Source: https://rungs.dev/docs/structured-text/logical-operators

> Logical operators AND, OR, XOR, and NOT combine BOOL expressions in Structured Text on rungs.dev, and double as bitwise operators on numeric values for masking and flag manipulation.

Logical operators combine or invert `BOOL` expressions that evaluate to `1` or `0`. They are essential for writing expressive, multi-condition IF statements. The same operators also work on numbers, where they operate [bit by bit](#bitwise-operations-on-numbers).

## Operators [#operators]

| Operator    | Description                                       | Example   |
| ----------- | ------------------------------------------------- | --------- |
| `AND` / `&` | `1` when both operands are `1`, else `0`          | `A AND B` |
| `OR`        | `1` when either operand is `1`, else `0`          | `A OR B`  |
| `XOR`       | `1` when exactly one operand is `1`, else `0`     | `A XOR B` |
| `NOT`       | Inverts a `BOOL` — `1` becomes `0` and vice versa | `NOT A`   |

`&` is a shorthand spelling of `AND` — both compile to the same operation.

## Operator Use in IF Statements [#operator-use-in-if-statements]

Logical operators are typically used inside IF conditions to control flow based on multiple inputs or states.

### Example 1: Basic AND condition [#example-1-basic-and-condition]

```iecst

IF Start_Button AND NOT Stop_Button THEN
    Motor_Run := 1;
END_IF;
```

This will start the motor only if the start button is pressed and the stop button is not pressed.

### Example 2: Using OR [#example-2-using-or]

```iecst

IF Emergency_Stop OR Fault THEN
    System_Enabled := 0;
END_IF;
```

This disables the system if either the emergency stop or a fault is active.

### Example 3: Multiple combined conditions [#example-3-multiple-combined-conditions]

```iecst

IF (Level < 50) AND (Pump_Enabled OR Manual_Override) THEN
    Pump := 1;
END_IF;
```

This turns on the pump if the level is low and either the pump is enabled or overridden manually.

## Bitwise Operations on Numbers [#bitwise-operations-on-numbers]

When both operands are numeric (`DINT` or `REAL`), the same operators combine the individual bits of the two values instead of combining conditions — this is called a bitwise operation. It is the standard way to test or modify flag bits packed into a single `DINT`.

| Expression  | Result | Why                                                   |
| ----------- | ------ | ----------------------------------------------------- |
| `12 AND 10` | `8`    | binary `1100 AND 1010` keeps only shared bits: `1000` |
| `12 OR 10`  | `14`   | `1100 OR 1010` keeps bits set in either: `1110`       |
| `12 XOR 10` | `6`    | `1100 XOR 1010` keeps bits that differ: `0110`        |
| `NOT 12`    | `-13`  | flips all 32 bits (one's complement)                  |

[Binary literals](https://rungs.dev/docs/tags/data-types#writing-dint-literals) make bit masks readable:

```iecst

Status_Bits := Inputs AND 2#0000_1111;
```

Rules to remember:

- `BOOL` and numeric operands never mix. `Flag AND Count` is a compile error — compare the number first: `Flag AND (Count > 0)`.
- A bitwise result is a `DINT`, not a `BOOL`. To use one in an IF condition, compare it: `IF (Inputs AND 2#0100) <> 0 THEN`.
- `REAL` operands are converted to `DINT` first using round-half-to-even, then the bits are combined.
- On numbers, `NOT` flips bits rather than inverting a condition — `NOT 1` is `-2`, not `0`.

## Notes [#notes]

- Parentheses can be used to group logic for clarity and precedence.

- Avoid long, complex IF conditions without parentheses — they can become hard to read and debug.

> **IDEA**
>
> **Practice**
>
> Try boolean logic in the [Xor Gate exercise](https://rungs.dev/exercises/xor-gate) — light a lamp when exactly one of two buttons is pressed — and the [Voting Lamp exercise](https://rungs.dev/exercises/voting-lamp) — a 2-of-3 majority vote.

## Best Practices [#best-practices]

- Keep `BOOL` expressions readable and explicit.

- Use descriptive variable names so conditions make sense on their own.

- Use NOT carefully — it only applies to the next expression, so grouping with parentheses is often safer.