operators
int bits = 0b1010;
int masked = bits & 0x0F;
int shifted = bits << 2;
All three ran under a current standard compiler
Arithmetic
+ - * / %
Integer division truncates
7 / 2 is 3, not 3.5
That fact causes quiet bugs when someone expects a float
Bitwise
& | ^ ~ << >>
Use them for flags and masks
Shifting a signed value into the sign bit is undefined behavior, do not do it
Assignment shortcuts
+= -= <<= and friends
They evaluate the left side once
Useful when the left side has a side effect
Precedence
Parentheses are cheaper than debugging When in doubt, write them A dense expression that needs a mental precedence table is a maintenance debt
Next
Control flow in 04 control
Posted