From 3aa0d422a01c6797962b8bf65b8a3e6ad526e648 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 20 Jan 2014 06:49:00 +0100 Subject: naïve parity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/algorithms/bits/Bits.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src') diff --git a/src/algorithms/bits/Bits.java b/src/algorithms/bits/Bits.java index 8f794e3..87939f7 100644 --- a/src/algorithms/bits/Bits.java +++ b/src/algorithms/bits/Bits.java @@ -240,6 +240,23 @@ public class Bits value = (£{T})((value * £{L4}) >> ((£{S} - 1) * 8)); return value; /* Only applicable upto 128 bits */ } + + /** + * Compute the parity of all bits in an integer + * + * @param value The interger + * @return The parity + */ + public static £{T} parity_naïve(£{T} value) + { + £{T} rc = 0; + while (value != 0) + { + rc ^= 1; + value &= value - 1; + } + return rc; + } £>done } -- cgit v1.2.3-70-g09d2