@willow-dls/core
    Preparing search index...

    Class BitString

    This class stores an arbitrary yet fixed-width collection of bits which are sent to CircuitElements via CircuitBuses. Bit strings are immutable, but this class provides a number of useful functions for performing operations with bit strings. Indeed, most of the functionality of an ALU is actually implemented here, not in a dedicate ALU circuit element, making it easy to implement other types of ALUs with ease.

    The reason this class exists is to bypass entirely the JavaScript number data type for sending bits through the circuit simulation, because number imposes restrictions on how values can be interpretted, as well as the size of values. BitString imposes no such limitations; there is no practical size limit of a bit string.

    Additionally, bit strings are useful for interpretting a collection of bits as either a signed or an unsigned number. Since JavaScript has no concept of an unsigned number, it can be difficult to interpret results. This class takes care of translating the bits in the string into either a positive or a potentially negative JavaScript number automatically, subject, of course, to the aforementioned limitations of number.

    Index

    Constructors

    • Construct a new bit string from a native JavaScript string, optionally fixing the width of the string without having to specify all of the bits.

      Parameters

      • str: string

        A bit string, which can be specified in one of two ways:

        • As a binary string consisting of only 0s and 1s. This is most useful for creating bit strings with short lengths.
        • As a hexadecimal string consisting of the prefix 0x and then hexadecimal characters. This is most useful for creating bit strings with longer lengths.
      • width: number = 0

        An optional width specifier that fixes the width of the bit string to this value. If it is not provided, the width is determine automatically based on how many bits were provided in str. If it is specified, one of three things can happen:

        • The width exactly matches that of str: it is simply redundant, but still helpful for the sake of clarity.
        • The width is greater than that of str: str will be padded with zero bits to match the specified width.
        • The width is less than that of str: str will be truncated to only width least significant bits, discarding the upper bits.

      Returns BitString

    Methods

    • Add two bit strings together, performing the operationg A + B where A is the bit string this method is called on, and B is the passed parameter.

      Parameters

      • str: string | BitString

        The bit string to add to this one.

      Returns BitString

      A new bit string which is the sum of the two operands.

    • Bitwise AND two bit strings together, performing the operationg A & B where A is the bit string this method is called on, and B is the passed parameter.

      Parameters

      • str: string | BitString

        The bit string to AND with this one.

      Returns BitString

      A new bit string which is the bitwise AND of the two operands.

    • Determine if two bit strings are equal in value and in length. Bit strings are only equal if their lengths are identical and all of their bits match.

      Parameters

      • str: null | string | BitString

        The bit string to compare to. If null is provided, the result is always false.

      Returns boolean

      Whether or not the two bit strings are equal.

    • All bit strings have a fixed width. This method returns the fixed width of the bit string.

      Returns number

      The number of bits which make up this bit string.

    • Determine if this bit string is greater than another one, performing the operation A > B where A is the bit string this method is called on, and B is the passed parameter.

      Note

      The naive method for determining A > B in two's compliment is to perform A - B and then check that the most significant bit is 0. If it is, then A > B. This method implements this functionality in this manner, using sub, msb, and equals, which may not be the most computationally efficient implementation.

      Parameters

      • str: null | string | BitString

        The bit string to compare to this one.

      Returns boolean

      Whether or not this bit string is greater than the given one.

    • Determine if this bit string is less than another one, performing the operation A < B where A is the bit string this method is called on, and B is the passed parameter.

      Note

      The naive method for determining A < B in two's compliment is to perform A - B and then check that the most significant bit is 1. If it is, then A < B. This method implements this functionality in this manner, using sub, msb, and equals, which may not be the most computationally efficient implementation.

      Parameters

      • str: null | string | BitString

        The bit string to compare to this one.

      Returns boolean

      Whether or not this bit string is less than the given one.

    • A shortcut for truncate that extracts the n least significant bits from the bit string.

      Parameters

      • n: number

        How many least significant bits to grab.

      Returns BitString

      A new bit string which represents the least sigificant n bits of this bit string.

    • A shortcut for truncate that extracts the n most significant bits from the bit string.

      Parameters

      • n: number

        How many most significant bits to grab.

      Returns BitString

      A new bit string which represents the most sigificant n bits of this bit string.

    • Negate all of the bits in this bit string, performing the operation ~A where A is the bit string this method is called on.

      Returns BitString

      A new bit string which is the the bitwise negation of this one.

    • Bitwise OR two bit strings together, performing the operationg A | B where A is the bit string this method is called on, and B is the passed parameter.

      Parameters

      • str: string | BitString

        The bit string to OR with this one.

      Returns BitString

      A new bit string which is the bitwise OR of the two operands.

    • Pad this bit string, adding significant bits which are 0 (low) until the string is the given length.

      Parameters

      • width: number

        How many bits to truncate this bit string to. If this is less than or equal to the length of this bit string, then the whole string is returned.

      Returns BitString

      A bit string which has been padded with leading zero bits until it is the specified width.

    • Subtract two bit strings, performing the operationg A - B where A is the bit string this method is called on, and B is the passed parameter.

      Note

      The naive algorithm for implementing subtraction in digital logic is to add the two's compliment of the second operand. This method implements subtraction in this manner by using twosCompliment and add, which may not be the most computationally efficient way to implement this functionality.

      Parameters

      • str: string | BitString

        The bit string to subtract from this one.

      Returns BitString

      A new bit string which is the difference between the two operands.

    • Extract a portion of the bit string, exactly like JavaScript's String.substring(). In fact, this method uses String.substring() to implement this functionality.

      Parameters

      • start: number

        The starting index of the bits to extract.

      • Optionalend: number

        The ending index of the bits to extract.

      Returns BitString

      A new bit string representing the specified subset of this one.

    • The same as calling toString with no parameters. This is simply to make JavaScript happy when it wants to serialize objects as JSON.

      Returns string

      The result of toString().

    • Interpret this bit string as a signed number, assuming it is stored in two's compliment form.

      Warning

      The behavior of this function depends on that of JavaScript's parseInt() and on the maximum size of numbers in JavaScript. If the bit string is too large to be properly processed by parseInt(), undefined behavior will result. Consult the parseInt() documentation for its failure modes.

      Returns number

      A signed JavaScript number.

    • Convert this bit string to a native JavaScript string.

      Parameters

      • radix: number = 2

        The radix to use to convert the bit string to a native string. Bit strings are stored internally as binary, regardless of whether they were instantiated with binary or hexadecimal. By default, the radix is 2, so the internal binary representation can be returned immediately. However, if you specify 16 as the radix, you can get a hexadecimal string back.

        Note

        Only radices 2 and 16 are supported. All other values for the radix will throw an exception.

      Returns string

      A JavaScript string.

    • Interpret this bit string as an unsigned number.

      Warning

      The behavior of this function depends on that of JavaScript's parseInt() and on the maximum size of numbers in JavaScript. If the bit string is too large to be properly processed by parseInt(), undefined behavior will result. Consult the parseInt() documentation for its failure modes.

      Returns number

      An unsigned JavaScript number.

    • Truncate this bit string, taking either the most significant length bits or the least significant length bits.

      Parameters

      • length: number

        How many bits to truncate this bit string to. If this is greater than or equal to the length of this bitstring, then the whole string is returned.

      • upper: boolean = false

        Whether or not the upper bits should be grabbed or the lower bits should be grabbed.

      Returns BitString

      A new bit string which is the truncation of this one.

    • Compute the two's compliment of the given bit string.

      Note

      The naive algorithm for implementing two's compliment is to invert all of the bits and then add one. In other words, perform the operation ~A + 1. This method implements the two's compliment in this manner by using not and add. This may not be the most computationally efficient way to implement this functionality.

      Returns BitString

      A new bit string which is the two's compliment of this bit string.

    • Create a bit string that is composed entirely of 1s and is the given width.

      Parameters

      • width: number = 1

        The width of the bit string which should have all bits set high.

      Returns BitString

    • Create a bit string that is composed entirely of 0s and is the given width.

      Parameters

      • width: number = 1

        The width of the bit string which should have all bits set low.

      Returns BitString

    • Create a random bit string of the specified width, where bits are toggled randomly using Math.random().

      Warning

      Math.random() is not cryptographically secure. I can't imagine a scenario in which that would matter to a circuit simulation engine, because surely you aren't doing cryptography in an educational digital logic simulator, right? Nonetheless, keep in mind that the random numbers produced by this function have all of the same flaws as (or perhaps more flaws than) Math.random().

      Parameters

      • width: number = 1

      Returns BitString