diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-01-20 02:31:12 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-01-20 02:31:12 +0100 |
commit | bd2b53ccc493b4a9b82ba936ca23f17ca9cbde2f (patch) | |
tree | 96e4371cc218c5c8781693e3cba8ecb6eb12d732 /src/algorithms/bits | |
parent | add opposite sign detection (diff) | |
download | algorithms-and-data-structures-bd2b53ccc493b4a9b82ba936ca23f17ca9cbde2f.tar.gz algorithms-and-data-structures-bd2b53ccc493b4a9b82ba936ca23f17ca9cbde2f.tar.bz2 algorithms-and-data-structures-bd2b53ccc493b4a9b82ba936ca23f17ca9cbde2f.tar.xz |
m + add abs
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/algorithms/bits/Absolute.java | 44 | ||||
-rw-r--r-- | src/algorithms/bits/Signum.java | 2 |
2 files changed, 44 insertions, 2 deletions
diff --git a/src/algorithms/bits/Absolute.java b/src/algorithms/bits/Absolute.java new file mode 100644 index 0000000..2e694eb --- /dev/null +++ b/src/algorithms/bits/Absolute.java @@ -0,0 +1,44 @@ +/** + * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package algorithms.bits; + + +/** + * Compute the absolute value of an integer + */ +public class Absolute +{ +£<for T_S in byte_7 short_15 int_31 long_63; do + T=${T_S%_*} +£>S=${T_S#*_} + /** + * Compute the absolute value of an integer + * + * @param value The integers + * @return The absolute value, the most negative value will because of overflow return the value itself + */ + public static £{T} abs(£{T} value) + { + £{T} mask;; + return (value ^ (mask = value >> £{S})) - mask; + /* Or alternatively: + * (v + (mask = value >> £{S})) ^ mask + */ + } +£>done +} + diff --git a/src/algorithms/bits/Signum.java b/src/algorithms/bits/Signum.java index 515e86d..3744be7 100644 --- a/src/algorithms/bits/Signum.java +++ b/src/algorithms/bits/Signum.java @@ -16,8 +16,6 @@ */ package algorithms.bits; -import java.util.*; - /** * Compute the sign of an integer |