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.
A bit string, which can be specified in one of two ways:
0
s and 1
s. This is most useful for
creating bit strings with short lengths.0x
and then hexadecimal
characters. This is most useful for creating bit strings with longer lengths.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:
str
: it is simply redundant,
but still helpful for the sake of clarity.str
: str
will be padded with zero bits
to match the specified width.str
: str
will be truncated to only width
least
significant bits, discarding the upper bits.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.
The bit string to AND with this one.
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.
The bit string to compare to. If null
is provided, the result
is always false
.
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.
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.
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.
The bit string to compare to this one.
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.
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.
The bit string to compare to this one.
Whether or not this bit string is less than the given one.
Negate all of the bits in this bit string, performing the operation ~A
where A
is the bit string this method is called on.
A new bit string which is the the bitwise negation of this one.
Pad this bit string, adding significant bits which are 0
(low) until
the string is the given length.
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.
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.
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.
The bit string to subtract from this one.
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.
The starting index of the bits to extract.
Optional
end: numberThe ending index of the bits to extract.
A new bit string representing the specified subset of this one.
Interpret this bit string as a signed number, assuming it is stored in two's compliment form.
The behavior of this function depends on that of JavaScript's parseInt()
and on the maximum size of number
s 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.
A signed JavaScript number.
Convert this bit string to a native JavaScript string.
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.
Only radices 2
and 16
are supported. All other values for the
radix
will throw an exception.
A JavaScript string.
Interpret this bit string as an unsigned number.
The behavior of this function depends on that of JavaScript's parseInt()
and on the maximum size of number
s 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.
An unsigned JavaScript number.
Truncate this bit string, taking either the most significant length
bits
or the least significant length
bits.
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.
Whether or not the upper bits should be grabbed or the lower bits should be grabbed.
A new bit string which is the truncation of this one.
Compute the two's compliment of the given bit string.
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.
A new bit string which is the two's compliment of this bit string.
Static
highCreate a bit string that is composed entirely of 1
s and is the given width.
The width of the bit string which should have all bits set high.
Static
lowCreate a bit string that is composed entirely of 0
s and is the given width.
The width of the bit string which should have all bits set low.
Static
randCreate a random bit string of the specified width, where bits are toggled
randomly using Math.random()
.
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()
.
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, becausenumber
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 ofnumber
.