From 6bd47bbb86a9f8feef4a620c022c0ef41105f65c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 20 Jan 2014 01:56:15 +0100 Subject: add signum 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 | 89 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/algorithms/bits/Signum.java (limited to 'src/algorithms/bits') diff --git a/src/algorithms/bits/Signum.java b/src/algorithms/bits/Signum.java new file mode 100644 index 0000000..647076c --- /dev/null +++ b/src/algorithms/bits/Signum.java @@ -0,0 +1,89 @@ +/** + * 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 . + */ +package algorithms.bits; + +import java.util.*; + + +/** + * Compute the sign of an integer + */ +public class Signum +{ +£S=${T_S#*_} + /** + * Compute the sign of an integer + * + * @param value The integer + * @return {@code -1} if negative, otherwise {@code 0} + */ + public static int minus_zero(£{T} value) + { + return (int)(value >> £{S}); + /* In C, you can also do either of: + * -(value < 0) + * -(£{T})((unsigned £{T})((£{T})value) >> (sizeof(£{T}) * CHAR_BITS - 1)); + * The method used here is not portable in C + * CHAR_BITS should be defined as 8 if not already defined + */ + } + + /** + * Compute the sign of an integer + * + * @param value The integer + * @return {@code -1} if negative, otherwise {@code 1} + */ + public static int minus_plus(£{T} value) + { + return 1 | (int)(value >> £{S}); + } + + /** + * Compute the sign of an integer + * + * @param value The integer + * @return {@code -1} if negative, {@code 1} if positive, and {@code 0} if zero + */ + public static int minus_zero_plus(£{T} value) + { + return (int)(value >> £{S}) - (int)(-value >> £{S}); + /* In C, you can also do either of: + * (value != 0) | -(£{T})((unsigned int)((£{T})value) >> (sizeof(£{T}) * CHAR_BITS - 1)) + * (value != 0) | (value >> (sizeof(£{T}) * CHAR_BITS - 1)) + * (value > 0) - (value < 0) + */ + } + + /** + * Compute the sign of an integer + * + * @param value The integer + * @return {@code 0} if negative, otherwise {@code 1} + */ + public static int zero_plus(£{T} value) + { + return 1 ^ (1 & (int)(value >> £{S})); + /* In C, you can also do: + * 1 ^ ((unsigned £{T})value >> (sizeof(£{T}) * CHAR_BITS - 1)) + */ + } +£>done +} + -- cgit v1.2.3-70-g09d2