fix sign-extend logic
authoradam <adam@megacz.com>
Tue, 13 Feb 2007 14:38:39 +0000 (15:38 +0100)
committeradam <adam@megacz.com>
Tue, 13 Feb 2007 14:38:39 +0000 (15:38 +0100)
src/edu/berkeley/fleet/slipway/Client.java

index 1e8ee83..a30f31b 100644 (file)
@@ -15,11 +15,17 @@ public class Client extends FleetProcess {
         throw new RuntimeException("not implemented");
     }
 
+    public static long signExtend(long val) {
+        if ((val & (1L << 36)) != 0)
+            val = val | (0xffffffffffffffffL << 36);
+        return val;
+    }
+                                    
     public long readWord() {
         if (isTerminated())
             throw new RuntimeException("this fleet has been terminated");
         try {
-            return queue.take();
+            return signExtend(queue.take());
         } catch (InterruptedException e) { throw new RuntimeException(e); }
     }
 
@@ -57,7 +63,9 @@ public class Client extends FleetProcess {
                         for(int i=0; i<6; i++) {
                             val = is.read();
                             if (val==-1) break;
-                            result |= ((long)val) << (i * 8);
+                            long val2 = (val & 0xffL);
+                            val2 = val2 << (i * 8);
+                            result |= val2;
                         }
                         if (val==-1) break;
                         queue.put(result);