Here is the code:
unsigned int v; // word value to compute the parity of
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
return (0x6996 >> v) & 1;
It computes the parity of given word, v. What is the meaning of 0x6996?
The number 0x6996 in binary is 110100110010110.
Well the algorithm is compressing the 32-bit int into a 4-bit value of the same parity by successive bitwise ORs and then ANDing with
0xfso that there are only positive bits in the least-significant 4-bits. In other words after line 5,vwill be an int between 0 and 15 inclusive.It then shifts that magic number (
0x6996) to the right by this 0-16 value and returns only the least significant bit (& 1).That means that if there is a
1in thevbit position of0x6996then the computed parity bit is 1, otherwise it’s 0 – for example if in line 5vis calculated as 2 then ` is returned, if it was 3 then 0 would be returned.