Skip to content

Commit 0cc4036

Browse files
committed
add optimal parity
1 parent d81ae2e commit 0cc4036

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

primitive-types/cpp/parity.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,17 @@ short Parity(unsigned long x) {
77
x >>= 1;
88
}
99
return result;
10+
}
11+
12+
13+
/* Here is the CPP for the best possible coding solution. This is a fairly complicated method. Check page 30 for the
14+
* solution explanation */
15+
short Parity(unsigned long x) {
16+
x ^= x >> 32;
17+
x ^= x >> 16;
18+
x ^= x >> 8;
19+
x ^= x >> 4;
20+
x ^= x >> 2;
21+
x ^= x >> 1;
22+
return x & 0x1;
1023
}

0 commit comments

Comments
 (0)