> For the complete documentation index, see [llms.txt](https://guide.cronuszen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide.cronuszen.com/gpcscripting/gpc-script-guide/learn-gpc/arithmetic.md).

# Arithmetic

<table data-search="false"><thead><tr><th>Operator</th><th>Meaning</th><th>Example</th></tr></thead><tbody><tr><td><code>+</code></td><td>Add two values</td><td><code>total = a + b;</code></td></tr><tr><td><code>-</code></td><td>Subtract the right value from the left; unary <code>-</code> negates one value</td><td><code>difference = a - b;</code></td></tr><tr><td><code>*</code></td><td>Multiply</td><td><code>scaled = value * 125;</code></td></tr><tr><td><code>/</code></td><td>Integer division; any fractional remainder is discarded</td><td><code>10 / 3</code> produces <code>3</code></td></tr><tr><td><code>%</code></td><td>Remainder after integer division</td><td><code>10 % 3</code> produces <code>1</code></td></tr><tr><td><code>++</code></td><td>Add one</td><td><code>index++;</code></td></tr><tr><td><code>--</code></td><td>Subtract one</td><td><code>index--;</code></td></tr></tbody></table>

Prefix changes the variable before the expression result is used; postfix returns the old value and then changes the variable:

```cpp
b = ++a; // increment a first, then assign the new value to b
b = a++; // assign the old value to b, then increment a
```

{% hint style="warning" %}
**Porting an older script?**

The original Zen Studio compiler assigned the **new** value here — `b = a++` gave `b` the incremented value. The current compiler assigns the old value, as described above.

Nothing warns you about this. A script carried over from the older compiler compiles cleanly and quietly computes something different. In a `return` statement the same difference does produce a warning, `GPC6029`, but in a plain assignment it does not.

Use the prefix form (`b = ++a;`) where you want the new value — it behaves identically on both compilers.
{% endhint %}

***

### Comparison

Comparison operators return true (`1`) or false (`0`):

| Operator | Meaning                  |
| -------- | ------------------------ |
| `==`     | Equal to                 |
| `!=`     | Not equal to             |
| `<`      | Less than                |
| `<=`     | Less than or equal to    |
| `>`      | Greater than             |
| `>=`     | Greater than or equal to |

***

### Logical

Logical operators treat zero as false and every nonzero value as true:

| Operator   | Meaning                                    |
| ---------- | ------------------------------------------ |
| `!a`       | Logical NOT: true when `a` is false        |
| `a && b`   | Logical AND: true when both are true       |
| `a \|\| b` | Logical OR: true when either is true       |
| `a ^^ b`   | Logical XOR: true when exactly one is true |
|            |                                            |

***

### Bitwise

Bitwise operators work on the individual bits of an integer:

| Operator | Meaning                                         |
| -------- | ----------------------------------------------- |
| `~a`     | Invert every bit                                |
| `a & b`  | Bitwise AND; keep bits set in both values       |
| `a \| b` | Bitwise OR; keep bits set in either value       |
| `a ^ b`  | Bitwise XOR; keep bits set in exactly one value |
| `a << n` | Shift bits left by `n` positions                |
| `a >> n` | Shift bits right by `n` positions               |

```cpp
flags = flags | 4;  // set bit 2
flags = flags & ~4; // clear bit 2
```

***

### Conditional

Zen Studio Live supports the ternary conditional operator. It chooses one of two expression values:

```cpp
result = condition ? value_if_true : value_if_false;
```

GPC evaluates `condition` first. A nonzero condition selects `value_if_true`; zero selects `value_if_false`.

{% hint style="info" %}
**Important:** only the selected branch is evaluated. If the condition is true, GPC evaluates `value_if_true` and does not evaluate `value_if_false`. If the condition is false, it evaluates `value_if_false` and does not evaluate `value_if_true`.
{% endhint %}

#### Choose a value

{% code title="ternary-choose-a-value.gpc" %}

```cpp
int enabled;
int output;

main {
    output = enabled ? 100 : 0;
    set_val(PS5_CROSS, output);
}
```

{% endcode %}

This is the expression form of a small `if` / `else` choice. The same logic written longhand is:

```cpp
if(enabled) {
    output = 100;
} else {
    output = 0;
}
```

#### The unused branch does not run

Short-circuit branch evaluation makes guarded expressions safe:

{% code title="guarded-division.gpc" %}

```cpp
int numerator = 100;
int divisor;
int result;

main {
    // When divisor is 0, only the : 0 branch runs.
    // numerator / divisor is not evaluated.
    result = divisor != 0 ? numerator / divisor : 0;
}
```

{% endcode %}

The rule also applies to function calls and other side effects:

{% code title="branch-side-effects.gpc" %}

```cpp
int use_a;
int a_calls;
int b_calls;
int output;

main {
    use_a = get_val(PS5_L2) > 0;
    output = use_a ? make_a() : make_b();

    // Only one counter increases during each main iteration.
    set_val(TRACE_1, a_calls);
    set_val(TRACE_2, b_calls);
}

function make_a() {
    a_calls++;
    return 100;
}

function make_b() {
    b_calls++;
    return 0;
}
```

{% endcode %}

When `use_a` is true, only `make_a()` runs. When it is false, only `make_b()` runs. Parenthesize nested conditional expressions and keep side effects obvious so the code remains easy to review.

***

### Assignment

Assignment stores the right-hand result in the writable variable or array element on the left. `=` means "gets the value," not "is equal to." Use `==` for comparison.

Compound assignment reads the current left-hand value, applies an operation, and writes the result back:

| Operator    | Equivalent form | Meaning                |
| ----------- | --------------- | ---------------------- |
| `a = b`     | —               | Assign `b` to `a`      |
| `a += b`    | `a = a + b`     | Add and assign         |
| `a -= b`    | `a = a - b`     | Subtract and assign    |
| `a *= b`    | `a = a * b`     | Multiply and assign    |
| `a /= b`    | `a = a / b`     | Divide and assign      |
| `a %= b`    | `a = a % b`     | Remainder and assign   |
| `a &= b`    | `a = a & b`     | Bitwise AND and assign |
| `a \|= b`   | `a = a \| b`    | Bitwise OR and assign  |
| `a ^= b`    | `a = a ^ b`     | Bitwise XOR and assign |
| `a <<= b`   | `a = a << b`    | Shift left and assign  |
| `a >>= b`   | `a = a >> b`    | Shift right and assign |
| `a &&= b`   | `a = a && b`    | Logical AND and assign |
| `a \|\|= b` | `a = a \|\| b`  | Logical OR and assign  |
| `a ^^= b`   | `a = a ^^ b`    | Logical XOR and assign |

Assignment associates from right to left, so `a = b = 5;` assigns `5` to `b` and then assigns that result to `a`. Prefer one assignment per line when the chained form would hide intent.

Parenthesize mixed bitwise/comparison expressions:

```cpp
if((flags & MASK) == MASK) { }
```

Without parentheses, equality binds more tightly than bitwise AND.

See the exact precedence table in the language reference.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://guide.cronuszen.com/gpcscripting/gpc-script-guide/learn-gpc/arithmetic.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
