From: Christian Femers Date: Thu, 11 Jul 2019 19:51:38 +0000 (+0200) Subject: Refactored BitVector methods to resolve ambiguity X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;ds=inline;h=2f2269e36940705063adba3ff89ed7830c0b2edf;p=Mograsim.git Refactored BitVector methods to resolve ambiguity --- diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java index 4fce8dde..8bff357a 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java @@ -91,6 +91,11 @@ public enum Bit implements StrictLogicType 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); diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java index 9e09f43a..cce83f43 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java @@ -13,7 +13,6 @@ import java.util.function.UnaryOperator; /** * Immutable class representing a {@link Bit}Vector * - * * @author Christian Femers * */ @@ -23,7 +22,7 @@ public final class BitVector implements StrictLogicType, Iterable, IterablebitIndex. (leftmost bit of a binary number at the given index) + */ + public Bit getMSBit(int bitIndex) { return bits[bitIndex]; } + /** + * Returns the least significant bit at bitIndex. (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(); @@ -224,16 +234,38 @@ public final class BitVector implements StrictLogicType, IterablebitIndex. (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 bitIndex. (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 bitIndex. (leftmost bit of a binary number at the given index) + */ + public Bit getMSBit(int bitIndex) { return bits[bitIndex]; } + /** + * Returns the least significant bit at bitIndex. (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; @@ -286,45 +318,26 @@ public final class BitVector implements StrictLogicType, Iterable, Iterablefrom MSB to LSB (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 iterator() { @@ -361,7 +362,7 @@ public final class BitVector implements StrictLogicType, Iterable