1 package edu.berkeley.fleet.ies44;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.*;
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);
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);
18 public static long getField(int highBit, int lowBit, long val) {
19 long mask = 0xffffffffffffffffL;
20 mask = mask << ((highBit-lowBit)+1);
22 mask = mask << lowBit;
23 long ret = val & mask;
28 public static long doPutField(int highBit, int lowBit, long val) {
29 long mask = 0xffffffffffffffffL;
30 mask = mask << (highBit-lowBit+1);
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);
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);