diff options
author | Christian Thalinger <twisti@complang.tuwien.ac.at> | 2008-07-01 11:10:29 +0000 |
---|---|---|
committer | Christian Thalinger <twisti@complang.tuwien.ac.at> | 2008-07-01 11:10:29 +0000 |
commit | deae25045cf8eef0e42bad1d2da7c23b1a5e7372 (patch) | |
tree | 12b0155cb51178c6dac04ddf29d99fa4c6782afe | |
parent | d05dd55aeda090ca43614ab61ff10cae889af0bf (diff) | |
download | classpath-deae25045cf8eef0e42bad1d2da7c23b1a5e7372.tar.gz |
2008-07-01 Christian Thalinger <twisti@complang.tuwien.ac.at>
* java/lang/Integer.java (signum): Implemented properly as
described in Hacker's Delight Section 2-7, plus Andrew Haley's
explanation.
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | java/lang/Integer.java | 9 |
2 files changed, 14 insertions, 1 deletions
@@ -1,3 +1,9 @@ +2008-07-01 Christian Thalinger <twisti@complang.tuwien.ac.at> + + * java/lang/Integer.java (signum): Implemented properly as + described in Hacker's Delight Section 2-7, plus Andrew Haley's + explanation. + 2008-07-01 Andrew Haley <aph@redhat.com> * java/lang/Long.java: Comment change only. diff --git a/java/lang/Integer.java b/java/lang/Integer.java index 83161b4bf..e5ca4b342 100644 --- a/java/lang/Integer.java +++ b/java/lang/Integer.java @@ -687,7 +687,14 @@ public final class Integer extends Number implements Comparable<Integer> */ public static int signum(int x) { - return (x >> 31) - (-x >> 31); + return (x >> 31) | (-x >>> 31); + + // The LHS propagates the sign bit through every bit in the word; + // if X < 0, every bit is set to 1, else 0. if X > 0, the RHS + // negates x and shifts the resulting 1 in the sign bit to the + // LSB, leaving every other bit 0. + + // Hacker's Delight, Section 2-7 } /** |