Golang operators: Difference between revisions
From wikinotes
No edit summary |
|||
Line 11: | Line 11: | ||
</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) | |||
</syntaxhighlight> | |||
</blockquote><!-- Bitwise Operators --> |
Revision as of 14:11, 29 May 2022
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)