diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-01-20 03:31:11 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-01-20 03:31:11 +0100 |
commit | 9531780a9b6874d22d779b2e17603a9324225c23 (patch) | |
tree | fb50987b20955bd0b8ade8642895d6ce651762d0 /src/algorithms/bits | |
parent | add bit merge (diff) | |
download | algorithms-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>
Diffstat (limited to '')
-rw-r--r-- | src/algorithms/bits/Signum.java | 29 |
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 } |