What is going on with bitwise operators and integer promotion?

[expr.unary.op] The operand of ~ shall have integral or unscoped enumeration type; the result is the one’s complement of its operand. Integral promotions are performed. [expr.shift] The shift operators << and >> group left-to-right. […] The operands shall be of integral or unscoped enumeration type and integral promotions are performed. What’s the integral promotion of uint8_t … Read more

Near constant time rotate that does not violate the standards

I’ve linked to this answer for the full details from several other “rotate” questions, including this community wiki question, which should be kept up to date with best-practices. I found a blog post about this issue, and it looks like it’s finally a solved problem (with new-enough compiler versions). John Regehr at the University of … Read more

Bitwise ‘&’ operator

5 is 101. 4 is 100. 5 & 4 is not 0: 101 100 & ↓↓↓ 100 Problem solved ✓ Clarification: In C, every non-zero value satisfies the if condition. Meaning, if you write: if (-5) { if (100) { // reachable code } } Whereas: if (0) { destroyTheWorld(); // we are safe }