1 package net.mograsim.logic.model.am2900.util;
3 import net.mograsim.logic.core.types.Bit;
4 import net.mograsim.logic.core.types.BitVector;
5 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
7 public final class TestUtil
15 * Transforms the last four bits of an int to a string that contains the binary ('1' and '0') representation of the 4 bits
17 * @author Christian Femers
19 public static String to4bitBin(int x)
21 StringBuilder sb = new StringBuilder(4);
22 sb.append((x & 0b1000) == 0 ? '0' : '1');
23 sb.append((x & 0b0100) == 0 ? '0' : '1');
24 sb.append((x & 0b0010) == 0 ? '0' : '1');
25 sb.append((x & 0b0001) == 0 ? '0' : '1');
30 * Transforms the given boolean to a string that contains the binary ('1' and '0') representation of the bit
32 * @author Christian Femers
34 public static String to1bitBin(boolean bitIsSet)
36 return bitIsSet ? "1" : "0";
40 * Transforms the given int to a string that contains the binary ('1' and '0') representation of the int. "0" is only returned when the
41 * int is equal to zero.
43 * @author Christian Femers
45 public static String to1bitBin(int someInt)
47 return someInt != 0 ? "1" : "0";
51 * Transforms a 4 bit signed integer (-8 to 7) to a int representing the same number. (Adding leading 1-bits if the 4 bit int is
54 * @author Christian Femers
56 public static int signed4ToSigned32(int signed4bit)
58 if ((signed4bit & 0b1000) > 0)
59 return signed4bit | 0xFF_FF_FF_F0;
60 return signed4bit & 0x00_00_00_0F;
64 * Transforms a 16 bit signed integer (-32768 to 32767 - a short) to a int representing the same number. (Adding leading 1-bits if the
65 * 16 bit int is negative)
67 * @author Christian Femers
69 public static int signed16ToSigned32(int signed16bit)
71 return (short) signed16bit;
75 * Transforms the last n bits of an int to a string that contains the binary ('1' and '0') representation of the n bits
77 * @author Christian Femers
79 public static String toNbitString(int x, int n)
81 StringBuilder sb = new StringBuilder(n);
82 for (int i = 0; i < n; i++)
84 sb.append((x >> i) & 1);
86 return sb.reverse().toString();
89 public static BitVector of(int value, int length)
91 BitVectorMutator mutator = BitVectorMutator.ofLength(length);
93 for (int i = length - 1; i >= 0; i--)
95 mutator.setMSBit(i, Bit.lastBitOf(val));
98 return mutator.toBitVector();