# 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

Syntax Relevant Types Description Example
a + ba += b
2+5==7
a +% ba +%= b
@as(u32, std.math.maxInt(u32))+%1==0
a - ba -= b
- Integers - FloatsSubtraction. - Can cause overflow for integers. - Invokes Peer Type Resolution for the operands. - See also @subWithOverflow.
2-5==-3
a -% ba -%= b
- IntegersWrapping Subtraction. - Guaranteed to have twos-complement wrapping behavior. - Invokes Peer Type Resolution for the operands. - See also @subWithOverflow.
@as(u32,0)-%1== std.math.maxInt(u32)
-a
- Integers - Floats Negation. - Can cause overflow for integers.
-1==0-1
-%a
- Integers Wrapping Negation. - Guaranteed to have twos-complement wrapping behavior.
-%@as(i32, std.math.minInt(i32))== std.math.minInt(i32)
a  ba = b
- Integers - FloatsMultiplication. - Can cause overflow for integers. - Invokes Peer Type Resolution for the operands. - See also @mulWithOverflow.
25==10
a % ba %= b
- IntegersWrapping Multiplication. - Guaranteed to have twos-complement wrapping behavior. - Invokes Peer Type Resolution for the operands. - See also @mulWithOverflow.
@as(u8,200)%2==144
a / ba /= b
- Integers - FloatsDivision. - Can cause overflow for integers. - Can cause Division by Zero for integers. - Can cause Division by Zero for floats in FloatMode.Optimized Mode. - For non-compile-time-known signed integers, must use @divTrunc, @divFloor, or @divExact instead of /.
- Invokes Peer Type Resolution for the operands.
10/5==2
a % ba %= b
- Integers - FloatsRemainder Division. - Can cause Division by Zero for integers. - Can cause Division by Zero for floats in FloatMode.Optimized Mode. - For non-compile-time-known signed integers, must use @rem or @mod instead of %.
- Invokes Peer Type Resolution for the operands.
10%3==1
a << ba <<= b
- IntegersBit Shift Left. - b must be comptime-known or have a type with log2 number of bits as a. - See also @shlExact. - See also @shlWithOverflow.
1<<8==256
a >> ba >>= b
- IntegersBit Shift Right. - b must be comptime-known or have a type with log2 number of bits as a. - See also @shrExact.
10>>1==5
a & ba &= b
- IntegersBitwise AND. - Invokes Peer Type Resolution for the operands.
0b011&0b101==0b001
a | ba |= b
- IntegersBitwise OR. - Invokes Peer Type Resolution for the operands.
0b010|0b100==0b110
a ^ ba ^= b
- IntegersBitwise XOR. - Invokes Peer Type Resolution for the operands.
0b011^0b101==0b110
~a
- Integers Bitwise NOT.
~@as(u8,0b10101111)==0b01010000
a orelse b
- OptionalsIf a is null, returns b ("default value"), otherwise returns the unwrapped value of a. Note that b may be a value of type noreturn.
const value:?u32 =null;const unwrapped = value orelse 1234;unwrapped ==1234
a.?
- Optionals Equivalent to:
a orelse unreachable
const value:?u32 =5678;value.?==5678
a catch ba catch|err| b
- Error UnionsIf 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.
const value: anyerror!u32 = error.Broken;const unwrapped = value catch1234;unwrapped ==1234
a and b
- bool If a is false, returns false without evaluating b. Otherwise, returns b.
(falseandtrue)==false
a or b
- bool If a is true, returns true without evaluating b. Otherwise, returns b.
falseortrue==true
!a
- bool Boolean NOT.
!false==true
a == b
- Integers - Floats - bool - type Returns true if a and b are equal, otherwise returns false. Invokes Peer Type Resolution for the operands.
(1==1)==true
a ==null
- Optionals Returns true if a is null, otherwise returns false.
const value:?u32 =null;value ==null
a != b
- Integers - Floats - bool - type Returns false if a and b are equal, otherwise returns true. Invokes Peer Type Resolution for the operands.
(1!=1)==false
a > b
- Integers - Floats Returns true if a is greater than b, otherwise returns false. Invokes Peer Type Resolution for the operands.
(2>1)==true
a >= b
- Integers - Floats Returns true if a is greater than or equal to b, otherwise returns false. Invokes Peer Type Resolution for the operands.
(2>=1)==true
a < b
- Integers - Floats Returns true if a is less than b, otherwise returns false. Invokes Peer Type Resolution for the operands.
(1<2)==true>
a <= b
- Integers - Floats Returns true if a is less than or equal to b, otherwise returns false. Invokes Peer Type Resolution for the operands.
(1<=2)==true
a ++ b
- Arrays Array concatenation. - Only available when a and b are compile-time known.
const mem =@import("std").mem;const array1 =[]u32{1,2};const array2 =[]u32{3,4};const together = array1 ++ array2;mem.eql(u32, together,&[_]u32{1,2,3,4})
a  b
- Arrays Array multiplication. - Only available when a and b are compile-time known.
const mem =@import("std").mem;const pattern ="ab"3;mem.eql(u8, pattern,"ababab")
a.
- Pointers Pointer dereference.
const x: u32 =1234;const ptr =&x;ptr.==1234
&a
const x: u32 =1234;const ptr =&x;ptr.*==1234
a || b
- Error Set TypeMerging Error Sets
const A = error{One};const B = error{Two};(A || B)== error{One,Two}

## Precedence

x() x[] x.ya!b!x -x -%x ~x &x ?xx{} x.* x.?!*/%***%||+-+++%-%<<>>&^|==!=<><=>=andororelse catch=*=/=%=+=-=<<=>>=&=^=|=