return values()[2 + (value & 1)];
}
+ public static Bit of(boolean binaryValue)
+ {
+ return binaryValue ? ONE : ZERO;
+ }
+
public static Bit parse(String s)
{
Bit bit = SYMBOL_MAP.get(s);
/**
* Immutable class representing a {@link Bit}Vector
*
- *
* @author Christian Femers
*
*/
private BitVector(Bit[] bits)
{
- this.bits = Objects.requireNonNull(bits);// do this first to "catch" bits==null before the foreach loop
+ this.bits = Objects.requireNonNull(bits); // do this first to "catch" bits==null before the foreach loop
for (Bit bit : bits)
if (bit == null)
throw new NullPointerException();
return BitVectorMutator.of(this);
}
- public Bit getBit(int bitIndex)
+ /**
+ * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+ */
+ public Bit getMSBit(int bitIndex)
{
return bits[bitIndex];
}
+ /**
+ * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+ */
+ public Bit getLSBit(int bitIndex)
+ {
+ return bits[bits.length - bitIndex - 1];
+ }
+
public Bit[] getBits()
{
return bits.clone();
return this;
}
- public void setBit(int bitIndex, Bit bit)
+ /**
+ * Set the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+ */
+ public void setMSBit(int bitIndex, Bit bit)
{
bits[bitIndex] = bit;
}
- public Bit getBit(int bitIndex)
+ /**
+ * Set the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+ */
+ public void setLSBit(int bitIndex, Bit bit)
+ {
+ bits[bits.length - bitIndex - 1] = bit;
+ }
+
+ /**
+ * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+ */
+ public Bit getMSBit(int bitIndex)
{
return bits[bitIndex];
}
+ /**
+ * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+ */
+ public Bit getLSBit(int bitIndex)
+ {
+ return bits[bits.length - bitIndex - 1];
+ }
+
public int length()
{
return bits.length;
return Arrays.equals(bits, offset, offset + other.length(), other.bits, 0, other.length());
}
- @Override
- public String toString()
- {
- return toBitStringMSBFirst();
- }
-
- /**
- * All {@link Bit}s symbols concatenated together
- *
- * @see #parse(String)
- */
- public String toBitStringLSBFirst()
- {
- StringBuilder sb = new StringBuilder(bits.length);
- for (Bit bit : bits)
- sb.append(bit);
- return sb.toString();
- }
-
/**
- * All {@link Bit}s symbols concatenated together, with the MSB coming first (like a binary number)
+ * All {@link Bit}s symbols concatenated together (MSB first)
*
* @see #parse(String)
*/
- public String toBitStringMSBFirst()
+ @Override
+ public String toString()
{
StringBuilder sb = new StringBuilder(bits.length);
for (Bit bit : bits)
sb.append(bit);
- sb.reverse();
return sb.toString();
}
/**
- * Parses a String containing solely {@link Bit} symbols
+ * Parses a String containing solely {@link Bit} symbols (MSB first)
*
- * @see #toBitStringLSBFirst()
+ * @see #toString()
*/
- public static BitVector parseLSBFirst(String s)
+ public static BitVector parse(String s)
{
Bit[] values = new Bit[s.length()];
for (int i = 0; i < s.length(); i++)
}
/**
- * Parses a String containing solely {@link Bit} symbols, with the MSB coming first (like a binary number)
- *
- * @see #toBitStringLSBFirst()
+ * Iterate over the {@link Bit}s of the BitVector <b>from MSB to LSB</b> (left to right).
*/
- public static BitVector parseMSBFirst(String s)
- {
- Bit[] values = new Bit[s.length()];
- for (int i = 0, j = s.length() - 1; j >= 0; i++, j--)
- {
- values[i] = Bit.parse(s, j);
- }
- return new BitVector(values);
- }
-
@Override
public Iterator<Bit> iterator()
{
{
if (!hasNext())
throw new NoSuchElementException();
- return getBit(pos++);
+ return getMSBit(pos++);
}
@Override
{\r
if (bitVector == null)\r
return "null";\r
- return bitVector.toBitStringMSBFirst();\r
+ return bitVector.toString();\r
}\r
\r
// TODO doesn't this belong to logic.ui?\r
// TODO maybe find a color assignment for multiple-bit bit vectors?\r
if (bitVector == null || bitVector.length() != 1)\r
return new ColorDefinition(BuiltInColor.COLOR_BLACK);\r
- switch (bitVector.getBit(0))\r
+ switch (bitVector.getLSBit(0))\r
{\r
case ONE:\r
return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.one");\r
case ZERO:\r
return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.zero");\r
default:\r
- throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getBit(0));\r
+ throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getLSBit(0));\r
}\r
}\r
\r
return val;\r
}\r
\r
+ /**\r
+ * Returns the least significant bit (LSB)\r
+ */\r
public Bit getValue()\r
{\r
return getValue(0);\r
}\r
\r
+ /**\r
+ * Returns the least significant bit (LSB) of the given index\r
+ */\r
public Bit getValue(int index)\r
{\r
- return values.getBit(index);\r
+ return values.getLSBit(index);\r
}\r
\r
public BitVector getValues(int start, int end)\r
}\r
\r
/**\r
- * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.\r
+ * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least\r
+ * significant bit (LSB)\r
*/\r
public Bit getInputValue()\r
{\r
\r
/**\r
* @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.\r
+ * Returns the least significant bit (LSB)\r
+ * \r
*/\r
public Bit getInputValue(int index)\r
{\r
- return inputValues.getBit(index);\r
+ return inputValues.getLSBit(index);\r
}\r
\r
/**\r
*/\r
public BitVector getInputValues()\r
{\r
- return getInputValues(0, length);\r
+ return inputValues;\r
}\r
\r
public BitVector getInputValues(int start, int end)\r
{
case "q":
if (wireQ.hasLogicModelBinding())
- return wireQ.getWireValues().getBit(0);
+ return wireQ.getWireValues().getLSBit(0);
return null;
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
break;
case "q":
BitVector newStateCasted = (BitVector) newState;
- setHighLevelState("q1", newStateCasted.getBit(0));
- setHighLevelState("q2", newStateCasted.getBit(1));
- setHighLevelState("q3", newStateCasted.getBit(2));
- setHighLevelState("q4", newStateCasted.getBit(3));
+ setHighLevelState("q1", newStateCasted.getLSBit(0));
+ setHighLevelState("q2", newStateCasted.getLSBit(1));
+ setHighLevelState("q3", newStateCasted.getLSBit(2));
+ setHighLevelState("q4", newStateCasted.getLSBit(3));
break;
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
Bit q2 = (Bit) getHighLevelState("q2");
Bit q3 = (Bit) getHighLevelState("q3");
Bit q4 = (Bit) getHighLevelState("q4");
- return BitVector.of(q1, q2, q3, q4);
+ return BitVector.of(q4, q3, q2, q1);
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
throw new IllegalStateException("Illegal atomic state ID: " + stateID);
break;
case "q":
BitVector newStateCasted = (BitVector) newState;
- setHighLevelState("q1", newStateCasted.getBit(0));
- setHighLevelState("q2", newStateCasted.getBit(1));
- setHighLevelState("q3", newStateCasted.getBit(2));
- setHighLevelState("q4", newStateCasted.getBit(3));
+ setHighLevelState("q1", newStateCasted.getLSBit(0));
+ setHighLevelState("q2", newStateCasted.getLSBit(1));
+ setHighLevelState("q3", newStateCasted.getLSBit(2));
+ setHighLevelState("q4", newStateCasted.getLSBit(3));
break;
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
Bit q2 = (Bit) getHighLevelState("q2");
Bit q3 = (Bit) getHighLevelState("q3");
Bit q4 = (Bit) getHighLevelState("q4");
- return BitVector.of(q1, q2, q3, q4);
+ return BitVector.of(q4, q3, q2, q1);
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
throw new IllegalStateException("Illegal atomic state ID: " + stateID);
if (radioBit.getSelection())
value = Bit.parse(valueString);
else if (radioBitVector.getSelection())
- value = BitVector.parseMSBFirst(valueString);
+ value = BitVector.parse(valueString);
else
throw new RuntimeException("No value type selected");
target.setHighLevelState(stateIDText.getText(), value);