# Operators

There is no operator overloading. When you see an operator in Zig, you know that it is doing something from this table, and nothing else.

# Table of Operators

Name Syntax Types Remarks Example
Addition <br>a + b<br>a += b<br> - Integers
- Floats
- Can cause overflow for integers.
- Invokes Peer Type Resolution for the operands.
- See also @addWithOverflow.
<br>2 + 5 == 7<br>
Wrapping Addition <br>a +% b<br>a +%= b<br> - Integers - Twos-complement wrapping behavior.
- Invokes Peer Type Resolution for the operands.
- See also @addWithOverflow.
<br>@as(u32, 0xffffffff) +% 1 == 0<br>
Saturating Addition <br>a +| b<br>a +|= b<br> - Integers - Invokes Peer Type Resolution for the operands. <br>@as(u8, 255) +| 1 == @as(u8, 255)<br>
Subtraction <br>a - b<br>a -= b<br> - Integers
- Floats
- Can cause overflow for integers.
- Invokes Peer Type Resolution for the operands.
- See also @subWithOverflow.
<br>2 - 5 == -3<br>
Wrapping Subtraction <br>a -% b<br>a -%= b<br> - Integers - Twos-complement wrapping behavior.
- Invokes Peer Type Resolution for the operands.
- See also @subWithOverflow.
<br>@as(u8, 0) -% 1 == 255<br>
Saturating Subtraction <br>a -| b<br>a -|= b<br> - Integers - Invokes Peer Type Resolution for the operands. <br>@as(u32, 0) -| 1 == 0<br>
Negation <br>-a<br> - Integers
- Floats
- Can cause overflow for integers. <br>-1 == 0 - 1<br>
Wrapping Negation <br>-%a<br> - Integers - Twos-complement wrapping behavior. <br>-%@as(i8, -128) == -128<br>
Multiplication <br>a * b<br>a *= b<br> - Integers
- Floats
- Can cause overflow for integers.
- Invokes Peer Type Resolution for the operands.
- See also @mulWithOverflow.
<br>2 * 5 == 10<br>
Wrapping Multiplication <br>a *% b<br>a *%= b<br> - Integers - Twos-complement wrapping behavior.
- Invokes Peer Type Resolution for the operands.
- See also @mulWithOverflow.
<br>@as(u8, 200) *% 2 == 144<br>
Saturating Multiplication <br>a *| b<br>a *|= b<br> - Integers - Invokes Peer Type Resolution for the operands. <br>@as(u8, 200) *| 2 == 255<br>
Division <br>a / b<br>a /= b<br> - Integers
- Floats
- Can cause overflow for integers.
- Can cause Division by Zero for integers.
- Can cause Division by Zero for floats in FloatMode.Optimized Mode.
- Signed integer operands must be comptime-known and positive. In other cases, use
@divTrunc,
@divFloor, or
@divExact instead.

- Invokes Peer Type Resolution for the operands.
<br>10 / 5 == 2<br>
Remainder Division <br>a % b<br>a %= b<br> - Integers
- Floats
- Can cause Division by Zero for integers.
- Can cause Division by Zero for floats in FloatMode.Optimized Mode.
- Signed or floating-point operands must be comptime-known and positive. In other cases, use
@rem or
@mod instead.

- Invokes Peer Type Resolution for the operands.
<br>10 % 3 == 1<br>
Bit Shift Left <br>a << b<br>a <<= b<br> - Integers - Moves all bits to the left, inserting new zeroes at the
least-significant bit.
- b must be
comptime-known or have a type with log2 number
of bits as a.
- See also @shlExact.
- See also @shlWithOverflow.
<br>0b1 << 8 == 0b100000000<br>
Saturating Bit Shift Left <br>a <<| b<br>a <<|= b<br> - Integers - See also @shlExact.
- See also @shlWithOverflow.
<br>@as(u8, 1) <<| 8 == 255<br>
Bit Shift Right <br>a >> b<br>a >>= b<br> - Integers - Moves all bits to the right, inserting zeroes at the most-significant bit.
- b must be
comptime-known or have a type with log2 number
of bits as a.
- See also @shrExact.
<br>0b1010 >> 1 == 0b101<br>
Bitwise And <br>a & b<br>a &= b<br> - Integers - Invokes Peer Type Resolution for the operands. <br>0b011 & 0b101 == 0b001<br>
Bitwise Or <br>a | b<br>a |= b<br> - Integers - Invokes Peer Type Resolution for the operands. <br>0b010 | 0b100 == 0b110<br>
Bitwise Xor <br>a ^ b<br>a ^= b<br> - Integers - Invokes Peer Type Resolution for the operands. <br>0b011 ^ 0b101 == 0b110<br>
Bitwise Not <br>~a<br> - Integers <br>~@as(u8, 0b10101111) == 0b01010000<br>
Defaulting Optional Unwrap <br>a orelse b<br> - Optionals If a is null,
returns b ("default value"),
otherwise returns the unwrapped value of a.
Note that b may be a value of type noreturn.
<br>const value: ?u32 = null;<br>const unwrapped = value orelse 1234;<br>unwrapped == 1234<br>
Optional Unwrap <br>a.?<br> - Optionals Equivalent to:

<br>a orelse unreachable<br>
<br>const value: ?u32 = 5678;<br>value.? == 5678<br>
Defaulting Error Unwrap <br>a catch b<br>a catch |err| b<br> - Error Unions If a is an error,
returns b ("default value"),
otherwise returns the unwrapped value of a.
Note that b may be a value of type noreturn.
err is the error and is in scope of the expression b.
<br>const value: anyerror!u32 = error.Broken;<br>const unwrapped = value catch 1234;<br>unwrapped == 1234<br>
Logical And <br>a and b<br> - bool If a is false, returns false
without evaluating b. Otherwise, returns b.
<br>(false and true) == false<br>
Logical Or <br>a or b<br> - bool If a is true,
returns true without evaluating
b. Otherwise, returns
b.
<br>(false or true) == true<br>
Boolean Not <br>!a<br> - bool <br>!false == true<br>
Equality <br>a == b<br> - Integers
- Floats
- bool
- type
- packed struct
Returns true if a and b are equal, otherwise returns false.
Invokes Peer Type Resolution for the operands.
<br>(1 == 1) == true<br>
Null Check <br>a == null<br> - Optionals Returns true if a is null, otherwise returns false. <br>const value: ?u32 = null;<br>(value == null) == true<br>
Inequality <br>a != b<br> - Integers
- Floats
- bool
- type
Returns false if a and b are equal, otherwise returns true.
Invokes Peer Type Resolution for the operands.
<br>(1 != 1) == false<br>
Non-Null Check <br>a != null<br> - Optionals Returns false if a is null, otherwise returns true. <br>const value: ?u32 = null;<br>(value != null) == false<br>
Greater Than <br>a > b<br> - Integers
- Floats
Returns true if a is greater than b, otherwise returns false.
Invokes Peer Type Resolution for the operands.
<br>(2 > 1) == true<br>
Greater or Equal <br>a >= b<br> - Integers
- Floats
Returns true if a is greater than or equal to b, otherwise returns false.
Invokes Peer Type Resolution for the operands.
<br>(2 >= 1) == true<br>
Less Than <br>a < b<br> - Integers
- Floats
Returns true if a is less than b, otherwise returns false.
Invokes Peer Type Resolution for the operands.
<br>(1 < 2) == true<br>
Lesser or Equal <br>a <= b<br> - Integers
- Floats
Returns true if a is less than or equal to b, otherwise returns false.
Invokes Peer Type Resolution for the operands.
<br>(1 <= 2) == true<br>
Array Concatenation <br>a ++ b<br> - Arrays - Only available when the lengths of both a and b are compile-time known. <br>const mem = @import("std").mem;<br>const array1 = [_]u32{1,2};<br>const array2 = [_]u32{3,4};<br>const together = array1 ++ array2;<br>mem.eql(u32, &together, &[_]u32{1,2,3,4})<br>
Array Multiplication <br>a ** b<br> - Arrays - Only available when the length of a and b are compile-time known. <br>const mem = @import("std").mem;<br>const pattern = "ab" ** 3;<br>mem.eql(u8, pattern, "ababab")<br>
Pointer Dereference <br>a.*<br> - Pointers Pointer dereference. <br>const x: u32 = 1234;<br>const ptr = &x;<br>ptr.* == 1234<br>
Address Of <br>&a<br> All types <br>const x: u32 = 1234;<br>const ptr = &x;<br>ptr.* == 1234<br>
Error Set Merge <br>a || b<br> - Error Set Type Merging Error Sets <br>const A = error{One};<br>const B = error{Two};<br>(A || B) == error{One, Two}<br>

# Precedence

x() x[] x.y x.* x.?
a!b
x{}
!x -x -%x ~x &x ?x
* / % ** *% *| ||
+ - ++ +% -% +| -|
<< >> <<|
& ^ | orelse catch
== != < > <= >=
and
or
= *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |=