aboutsummaryrefslogtreecommitdiffstats
path: root/src/algorithms/bits
diff options
context:
space:
mode:
Diffstat (limited to 'src/algorithms/bits')
-rw-r--r--src/algorithms/bits/Absolute.java44
-rw-r--r--src/algorithms/bits/Signum.java2
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