aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-01-20 03:31:11 +0100
committerMattias Andrée <maandree@operamail.com>2014-01-20 03:31:11 +0100
commit9531780a9b6874d22d779b2e17603a9324225c23 (patch)
treefb50987b20955bd0b8ade8642895d6ce651762d0
parentadd bit merge (diff)
downloadalgorithms-and-data-structures-9531780a9b6874d22d779b2e17603a9324225c23.tar.gz
algorithms-and-data-structures-9531780a9b6874d22d779b2e17603a9324225c23.tar.bz2
algorithms-and-data-structures-9531780a9b6874d22d779b2e17603a9324225c23.tar.xz
add conditional negation
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--src/algorithms/bits/Signum.java29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/algorithms/bits/Signum.java b/src/algorithms/bits/Signum.java
index 07ddddf..d33323a 100644
--- a/src/algorithms/bits/Signum.java
+++ b/src/algorithms/bits/Signum.java
@@ -18,7 +18,7 @@ package algorithms.bits;
/**
- * Compute the sign of an integer
+ * Signum operations on integers
*/
public class Signum
{
@@ -92,8 +92,33 @@ public class Signum
*/
public static boolean haveOpposite(£{T} a, £{T} b)
{
- return (a ^ b) < 0;
+ return (£{T})(a ^ b) < 0;
}
+
+ /**
+ * Negates a value of a flag is set to 1
+ *
+ * @param value The value
+ * @param flag The flag, must be either 1 or 0
+ * @return {@code flag == 1 ? value : -value}
+ */
+ public static £{T} negateOn1(£{T} value, £{T} flag)
+ {
+ return (£{T})((£{T})(flag ^ (flag - 1)) * value);
+ }
+
+ /**
+ * Negates a value of a flag is set to 1
+ *
+ * @param value The value
+ * @param flag The flag, must be either 1 or 0
+ * @return {@code flag == 0 ? value : -value}
+ */
+ public static £{T} negateOn0(£{T} value, £{T} flag)
+ {
+ return (£{T})((£{T})(value ^ -flag) + flag);
+ }
+
£>done
}