diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-01-20 07:11:57 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-01-20 07:11:57 +0100 |
commit | 3a5382a0117dbe7d49fda0cd7ef7c780b51de9ba (patch) | |
tree | 469e0496dd2598a9e5a022958889b99aef0beaa0 | |
parent | 64-bit instructions for parity on byte (diff) | |
download | algorithms-and-data-structures-3a5382a0117dbe7d49fda0cd7ef7c780b51de9ba.tar.gz algorithms-and-data-structures-3a5382a0117dbe7d49fda0cd7ef7c780b51de9ba.tar.bz2 algorithms-and-data-structures-3a5382a0117dbe7d49fda0cd7ef7c780b51de9ba.tar.xz |
parity via multiplication
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/algorithms/bits/Bits.java | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/algorithms/bits/Bits.java b/src/algorithms/bits/Bits.java index 3101217..ace5fed 100644 --- a/src/algorithms/bits/Bits.java +++ b/src/algorithms/bits/Bits.java @@ -343,6 +343,20 @@ public class Bits value ^= value >> 4; return (0x6996 >> (value & 15)) & 1; } + + /** + * Compute the parity of all bits in an integer, multiplication version + * + * @param value The interger + * @return The parity + */ + public static £{T} parity_multiplication(£{T} value) + { + value ^= value >> 1; + value ^= value >> 2; + value = (£{T})((value & (£{T})0x1111111111111111L) * (£{T})0x1111111111111111L); + return (£{T})((value >> (£{S} - 4)) & 1); + } £>done } |