aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-01-20 06:49:00 +0100
committerMattias Andrée <maandree@operamail.com>2014-01-20 06:49:00 +0100
commit3aa0d422a01c6797962b8bf65b8a3e6ad526e648 (patch)
tree8cc88d57a3f24aa8ae901720df66d140a901250f
parentm (diff)
downloadalgorithms-and-data-structures-3aa0d422a01c6797962b8bf65b8a3e6ad526e648.tar.gz
algorithms-and-data-structures-3aa0d422a01c6797962b8bf65b8a3e6ad526e648.tar.bz2
algorithms-and-data-structures-3aa0d422a01c6797962b8bf65b8a3e6ad526e648.tar.xz
naïve parity
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--src/algorithms/bits/Bits.java17
1 files changed, 17 insertions, 0 deletions
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
}