Operations

Arithmetic Operations

%
  • Modulo (truncated).

  • %  is dividend

%%
  • Remainder (floored).

  • %%  is divisor.

  • For unsigned integers, %  and %%  are identical, but the difference comes when using signed integers.

Logical Operations

"Short-Circuit"
  • It means that if the first condition is false  then the second condition won't be evaluated.

  • This works for any control flow, as the "short-circuiting" is a property of the logical operators ( && , || ), not the control flow.

    • So this is also applicable to ternary operations, for example.

  • if a != nil && a.something == true {}

    • This is safe, as when the first condition is false , the second one will not be evaluated.

  • if a.something == true && a != nil {}

    • This is unsafe. The first condition will be evaluated first, so if a == nil , this will crash.

conditional AND ( && )
a && b  is  "b if a else false"
conditional OR ( || )
a || b  is  "true if a else b"

Bitwise Operations

OR ( | )
  • .

XOR ( ~ )
  • ~u32(0)  is effectively max(u32) .

AND ( & )
  • .

AND-NOT ( &~ )
  • .

LEFT SHIFT ( << )
  • .

RIGHT SHIFT ( >> )
  • .