Golang operators: Difference between revisions
From wikinotes
No edit summary |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Logical = | |||
<blockquote> | |||
<syntaxhighlight lang="go"> | |||
&& // and | |||
|| // or | |||
! // not | |||
</syntaxhighlight> | |||
</blockquote><!-- Logical --> | |||
= Arithmetic = | = Arithmetic = | ||
<blockquote> | <blockquote> | ||
Line 11: | Line 20: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Arithmetic --> | </blockquote><!-- Arithmetic --> | ||
= Bitwise Operators = | |||
<blockquote> | |||
<syntaxhighlight lang="go"> | |||
0b1111 & 0b0101 == 0b0101 // bitmask (bitwise and) | |||
0b0111 | 0b1100 == 0b1111 // set-bit (bitwise or) | |||
0b0011 ^ 0b1010 == 0b1001 // toggle-bit (bitwise xor) | |||
0b1100 &^ 0b0110 == 0b0001 // inverted bitmask (bitise and-not) | |||
0b0011 << 0b0001 == 0b0110 // bit-shift left | |||
0b0110 >> 0b0001 == 0b0011 // bit-shift right | |||
</syntaxhighlight> | |||
</blockquote><!-- Bitwise Operators --> |
Latest revision as of 23:53, 5 June 2022
Logical
&& // and || // or ! // not
Arithmetic
Go requires that all variables involved in arithmetic are the same type.
(ex. uint8 + unit16 would fail to compile)1 + 1 // addition 1 - 1 // subtraction 1 * 1 // multiplication 1 / 1 // division 1 % 1 // modulus
Bitwise Operators
0b1111 & 0b0101 == 0b0101 // bitmask (bitwise and) 0b0111 | 0b1100 == 0b1111 // set-bit (bitwise or) 0b0011 ^ 0b1010 == 0b1001 // toggle-bit (bitwise xor) 0b1100 &^ 0b0110 == 0b0001 // inverted bitmask (bitise and-not) 0b0011 << 0b0001 == 0b0110 // bit-shift left 0b0110 >> 0b0001 == 0b0011 // bit-shift right