It may be useful to note that when C++ sees a modulo 2
operation as %2
, it usually optimizes without you doing bitwise operations.
While it would be enlightening to understand all such tricks, it should be pleasing to know that the compiler (or the compiler writer) does hard work to get all optimizations possible.
What you should remember is, if you use constants and work in powers of 2, optimizations are more likely since the compilers leverage the machine's binary operator abilities.
Going further, I would suggest gaining knowledge of how systems work at low level.
To that end, learning tricks you refer to here would be very useful.
However, cryptic coding with complicated operations jammed together
(say, to do it all in less number of source code bytes) is no good.
It may be good to know that you can swap two 32-bit variables 'in place', without a third temporary variable -- using XOR operations. But, it would be tons more useful to know hhow cross compilation requires big-endian and little-endian handling for 2/4 byte variables and bit fields.
Talking about bit-fields, reminds me of another stackoverflow conversation on their popularity. Would also be good reading (though not entirely related to your question).
To summarize, I am totally with you in learning what tricks can be done. I want to use them to make my code perform better -- and, I strongly feel it will be concepts like, what can programmers to do make better cache optimization, for example, that will help making better implementations.