Solution with pure bits:. Repeat XOR the lower and upper half of your value as shown below:
function IsOdd(n) { n ^= n >> 32; n ^= n >> 16; n ^= n >> 8; n ^= n >> 4; n ^= n >> 2; n ^= n >> 1; return (n & 1) == 1; }
This can be optimized using a pre-populated lookup table:
function Prepopulate() { bool[] answer = new bool[256]; answer[0] = false; for (int i = 1; i < 256; i++) answer[i] = answer[i >> 1] ^ ((i & 1) == 1); } function IsOdd(n) { n ^= n >> 32; n ^= n >> 16; n ^= n >> 8; return answer[n & 255]; }
You might want to use different sizes of the filled tables; in my example, I used an 8-bit table (256 elements).
Tolya source share