I came across this piece of code on reddit
1 - ((num & 1) << 1) as i32
This code returns 1 for even numbers and -1 for odd numbers.
It takes less instructions than other ways of calculating the same thing, and is presumably, quite fast. So, how does it work? (A step-by-step breakdown would be helpful)
Note: I found What is the fastest way to find if a number is even or odd?, but don't understand how that works either.