3 import java.util.Arrays;
6 * stdlogic according to IEEE 1164
11 public static Bit and(Bit a, Bit b) {
15 public Bit and(Bit other) {
16 return fromTable(AND_TABLE, this, other);
19 public static Bit or(Bit a, Bit b) {
23 public Bit or(Bit other) {
24 return fromTable(OR_TABLE, this, other);
27 public static Bit xor(Bit a, Bit b) {
31 public Bit xor(Bit other) {
32 return fromTable(XOR_TABLE, this, other);
48 public Bit[] makeArray(int length) {
49 Bit[] bits = new Bit[length];
50 Arrays.fill(bits, this);
54 public Bit combineWith(Bit other) {
55 return fromTable(JOIN_TABLE, this, other);
58 public static Bit combine(Bit a, Bit b) {
59 return a.combineWith(b);
62 private static Bit fromTable(Bit[][] table, Bit a, Bit b) {
63 return table[a.ordinal()][b.ordinal()];
67 private static Bit[][] JOIN_TABLE =
70 { U, X, ZERO, X, ZERO },
71 { U, X, X, ONE, ONE },
72 { U, X, ZERO, ONE, Z } };
74 private static Bit[][] AND_TABLE =
75 { { U, U, ZERO, U, U },
77 { ZERO, ZERO, ZERO, ZERO, ZERO },
78 { U, X, ZERO, ONE, X },
79 { U, X, ZERO, X, X } };
81 private static Bit[][] OR_TABLE =
82 { { U, U, U, ONE, U },
84 { U, X, ZERO, ONE, X },
85 { ONE, ONE, ONE, ONE, ONE },
86 { U, X, X, ONE, X } };
88 private static Bit[][] XOR_TABLE =
91 { U, X, ZERO, ONE, X },
92 { U, X, ONE, ZERO, X },