remove Execute and Halt
[fleet.git] / src / edu / berkeley / fleet / ies44 / BitManipulations.java
1 package edu.berkeley.fleet.ies44;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.*;
4 import java.io.*;
5
6 public class BitManipulations {
7     public static boolean getBit(int bit, long val) { return ((val & (1L << bit)) != 0); }
8     public static long getSignedField(int highBit, int lowBit, long val) {
9         long ret = getField(highBit, lowBit, val);
10         if ((ret & (1L << ((highBit-lowBit)+1-1))) != 0)
11             ret |= 0xffffffffffffffffL << ((highBit-lowBit)+1);
12         return ret;
13     }
14     public static int getIntField(int highBit, int lowBit, long val) {
15         if (highBit-lowBit+1 > 32) throw new RuntimeException("too big!");
16         return (int)getField(highBit, lowBit, val);
17     }
18     public static long getField(int highBit, int lowBit, long val) {
19         long mask = 0xffffffffffffffffL;
20         mask = mask << ((highBit-lowBit)+1);
21         mask = ~mask;
22         mask = mask << lowBit;
23         long ret = val & mask;
24         ret = ret >> lowBit;
25         return ret;
26     }
27
28     public static long doPutField(int highBit, int lowBit, long val) {
29         long mask = 0xffffffffffffffffL;
30         mask = mask << (highBit-lowBit+1);
31         mask = ~mask;
32         val = val & mask;
33         val = val << lowBit;
34         return val;
35     }
36     public static long putField(int highBit, int lowBit, long val) {
37         if (val < 0 || val >= (1L << (highBit-lowBit+1)))
38             throw new RuntimeException("bitfield width exceeded");
39         return doPutField(highBit, lowBit, val);
40     }
41     public static long putSignedField(int highBit, int lowBit, long val) {
42         if (val <= (-1L * (1L << (highBit-lowBit+1-1))))
43             throw new RuntimeException("bitfield width exceeded");
44         if (val >= (      (1L << (highBit-lowBit+1-1))))
45             throw new RuntimeException("bitfield width exceeded");
46         return doPutField(highBit, lowBit, val);
47     }
48 }