Zig Operators

Operators are the symbols that perform actions on values. Zig includes arithmetic, comparison, logical, bitwise, and assignment operators. What makes Zig distinct is that it takes arithmetic overflow seriously — operations that produce a value outside the type's range are compile-time errors or runtime panics, not silent corruption.

Arithmetic Operators

  Operator | Meaning        | Example          | Result
  ---------+----------------+------------------+-------
  +        | Addition       | 10 + 3           | 13
  -        | Subtraction    | 10 - 3           | 7
  *        | Multiplication | 10 * 3           | 30
  /        | Division       | 10 / 3           | 3  (integer division)
  %        | Remainder      | 10 % 3           | 1

Integer division drops the decimal portion. Ten divided by three gives three, not 3.33. The remainder operator gives what is left over — ten divided by three leaves a remainder of one.

  Real-world model:

  10 cookies split among 3 people:
  +---------+---------+---------+
  | 3 each  | 3 each  | 3 each  |
  +---------+---------+---------+
  [ 1 cookie left over ]

  10 / 3 = 3   (each person gets 3)
  10 % 3 = 1   (1 cookie remains)

Overflow — Zig's Strict Approach

  var x: u8 = 250;
  x = x + 10;  // 260 does not fit in u8 (max 255)
               // → runtime panic in debug mode

In C, adding 10 to a u8 holding 250 silently wraps around to 4. This silent wrap is the source of countless bugs. Zig panics instead — it stops the program and tells you exactly where the overflow happened. If you intentionally want wrapping, Zig provides special operators:

  +%   wrapping add       (wraps around on overflow)
  -%   wrapping subtract  (wraps around on underflow)
  *%   wrapping multiply  (wraps around on overflow)
  +|   saturating add     (stops at max value)
  -|   saturating sub     (stops at min value)
  *|   saturating mul     (stops at max value)
  Wrapping:      250 +% 10 = 4     (wraps past 255 back to 4)
  Saturating:    250 +| 10 = 255   (stops at the ceiling)

Comparison Operators

Comparison operators produce a bool result — either true or false.

  Operator | Meaning               | Example   | Result
  ---------+-----------------------+-----------+-------
  ==       | Equal to              | 5 == 5    | true
  !=       | Not equal to          | 5 != 3    | true
  <        | Less than             | 3 < 5     | true
  >        | Greater than          | 5 > 3     | true
  <=       | Less than or equal    | 3 <= 3    | true
  >=       | Greater than or equal | 5 >= 6    | false
  Everyday model:

  Is the temperature above 37 degrees?
  temperature > 37

  If temperature = 38.5:
  38.5 > 37  →  true  (yes, it is a fever)

  If temperature = 36.8:
  36.8 > 37  →  false (no fever)

Logical Operators

Logical operators combine boolean values.

  Operator | Meaning                          | Symbol
  ---------+----------------------------------+-------
  and      | Both must be true                | and
  or       | At least one must be true        | or
  not      | Flips true to false and vice versa| !
  Truth table for AND:
  A     | B     | A and B
  ------+-------+--------
  true  | true  | true
  true  | false | false
  false | true  | false
  false | false | false

  Truth table for OR:
  A     | B     | A or B
  ------+-------+-------
  true  | true  | true
  true  | false | true
  false | true  | true
  false | false | false
  Example scenario:
  Grant access if the user is an admin AND is logged in.

  const is_admin = true;
  const is_logged_in = true;
  const can_access = is_admin and is_logged_in;  // true

  is_admin = false:
  const can_access = false and true;  // false → no access

Short-Circuit Evaluation

Zig uses short-circuit evaluation for and and or. With and, if the left side is false, Zig skips the right side entirely — the result is already false. With or, if the left side is true, Zig skips the right side — the result is already true. This matters when the right side has side effects or can fail.

Assignment Operators

  Operator | Meaning               | Equivalent to
  ---------+-----------------------+--------------
  =        | Assign                | x = 5
  +=       | Add and assign        | x = x + 5
  -=       | Subtract and assign   | x = x - 5
  *=       | Multiply and assign   | x = x * 5
  /=       | Divide and assign     | x = x / 5
  %=       | Remainder and assign  | x = x % 5
  var score: i32 = 100;
  score += 50;   // score is now 150
  score -= 20;   // score is now 130
  score *= 2;    // score is now 260

Bitwise Operators

Bitwise operators work directly on the binary bits of a number. These are used in systems programming, networking, and hardware control.

  Operator | Name         | Example (binary)
  ---------+--------------+------------------
  &        | AND          | 1010 & 1100 = 1000
  |        | OR           | 1010 | 1100 = 1110
  ^        | XOR          | 1010 ^ 1100 = 0110
  ~        | NOT (flip)   | ~1010 = 0101
  <<       | Left shift   | 0001 << 2 = 0100
  >>       | Right shift  | 1000 >> 2 = 0010
  Practical use — checking if a number is even or odd:

  5 in binary:  0000 0101
  1 in binary:  0000 0001
  5 & 1      =  0000 0001  → result 1 → odd

  4 in binary:  0000 0100
  1 in binary:  0000 0001
  4 & 1      =  0000 0000  → result 0 → even

Shift Operators — Multiply or Divide by Powers of Two

  Left shift  (<<): multiply by 2 for each shift position
  1 << 1 = 2
  1 << 2 = 4
  1 << 3 = 8

  Right shift (>>): divide by 2 for each shift position
  8 >> 1 = 4
  8 >> 2 = 2
  8 >> 3 = 1

Operator Precedence

When multiple operators appear in one expression, Zig evaluates them in a specific order. Use parentheses to make the order explicit and avoid confusion.

  2 + 3 * 4     →  14  (* runs first)
  (2 + 3) * 4   →  20  (+ runs first due to parentheses)
  High priority  →  Runs first:  * / % << >>
  Medium priority:               + -  | ^ &
  Low priority   →  Runs last:   == != < > <= >= and or

When in doubt, add parentheses. Clear code matters more than saving a few characters. The compiler does not care about extra parentheses, but future readers of your code will appreciate the clarity.

Leave a Comment

Your email address will not be published. Required fields are marked *