From 9531780a9b6874d22d779b2e17603a9324225c23 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 20 Jan 2014 03:31:11 +0100 Subject: add conditional negation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/algorithms/bits/Signum.java | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src') 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 } -- cgit v1.2.3-70-g09d2