937e566a807d2393cb5d0eb5f6899597e76ca755
[Mograsim.git] / net.mograsim.logic.model.am2900 / test / net / mograsim / logic / ui / am2900 / TestUtil.java
1 package net.mograsim.logic.ui.am2900;
2
3 public final class TestUtil
4 {
5         private TestUtil()
6         {
7
8         }
9
10         /**
11          * Transforms the last four bits of an int to a string that contains the binary ('1' and '0') representation of the 4 bits
12          * 
13          * @author Christian Femers
14          */
15         public static String to4bitBin(int x)
16         {
17                 StringBuilder sb = new StringBuilder(4);
18                 sb.append((x & 0b1000) == 0 ? '0' : '1');
19                 sb.append((x & 0b0100) == 0 ? '0' : '1');
20                 sb.append((x & 0b0010) == 0 ? '0' : '1');
21                 sb.append((x & 0b0001) == 0 ? '0' : '1');
22                 return sb.toString();
23         }
24
25         /**
26          * Transforms the given boolean to a string that contains the binary ('1' and '0') representation of the bit
27          * 
28          * @author Christian Femers
29          */
30         public static String to1bitBin(boolean bitIsSet)
31         {
32                 return bitIsSet ? "1" : "0";
33         }
34
35         /**
36          * Transforms the given int to a string that contains the binary ('1' and '0') representation of the int. "0" is only returned when the
37          * int is equal to zero.
38          * 
39          * @author Christian Femers
40          */
41         public static String to1bitBin(int someInt)
42         {
43                 return someInt != 0 ? "1" : "0";
44         }
45
46         /**
47          * 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
48          * negative)
49          * 
50          * @author Christian Femers
51          */
52         public static int signed4ToSigned32(int signed4bit)
53         {
54                 if ((signed4bit & 0b1000) > 0)
55                         return signed4bit | 0xFF_FF_FF_F0;
56                 return signed4bit & 0x00_00_00_0F;
57         }
58
59         /**
60          * Transforms a 16 bit signed integer (-32768 to 32767 - a short) to a int representing the same number. (Adding leading 1-bits if the
61          * 16 bit int is negative)
62          * 
63          * @author Christian Femers
64          */
65         public static int signed16ToSigned32(int signed16bit)
66         {
67                 return (short) signed16bit;
68         }
69
70         /**
71          * Transforms the last n bits of an int to a string that contains the binary ('1' and '0') representation of the n bits
72          * 
73          * @author Christian Femers
74          */
75         public static String toNbitString(int x, int n)
76         {
77                 StringBuilder sb = new StringBuilder(n);
78                 for (int i = 0; i < n; i++)
79                 {
80                         sb.append((x >> i) & 1);
81                 }
82                 return sb.reverse().toString();
83         }
84 }