Some would call it math.
But I guess that shows you the state of our art.
In most cases you won't see these alone. They will be surrounded by tens of thousands of other lines of code. And where there is one "hack" there are usually others. And at some point you end up with a bug somewhere because one of these smart little hacks is failing. You don't know which one.
This does create unnecessary problems for maintenance in the future. So use it, but use it only when absolutely necessary. Not because you think the code will run faster.
It's also important to note that many things people think make the code faster, don't. Like vector vs. linked list access. Intuitively the linked list seems much simpler for inserting an element. But in almost all cases the vector is faster, because of cache locality.
So what I'm saying is. Do things that make the code probably faster like using a vector, if they don't cloud understanding. If they do, leave them out until you have a reason to use them. The only reason to use micro-optimizations like these is if you measured execution time and know there is a performance bug in this place in the code which your "hack" could resolve.
No measurement -> no optimization. Because the performance problems are almost always in different places than the average developer assumes.
And then add a comment explaining what you did to a 5 year old.
Only if you make certain assumptions about how the integer is represented, I reckon.
int isEven(int number)
{
return number & 1;
}
gcc 7.3, -O1, -O2 and -O3 (identical codegen for all optimization levels): isEven(int):
mov eax, edi
and eax, 1
retAnyways, you still prove my point. All those three are pretty close.
-O1 sete instruction is very slightly unoptimal, but even it is just a fractional clock cycle.
test eax, 1
; use result is_even(int):
mov eax, edi
not eax
and eax, 1
ret
I am far from a x86 expert, but I assume this version avoids contention on the flags registers and increases pipelining.edit: my mistake, icc and clang produce the equivalent sequence "not edi; and edi, 1; mov eax, edi; ret".