Gets the hexadecimal string representation of the bit string.
The hexadecimal representation of the current bit string.
Gets the length of the binary string.
The number of characters in the binary string.
Adds the current BitString to another BitString and returns the result as a new BitString. The addition is performed bit by bit, starting from the least significant bit (rightmost), and handles carry-over similar to binary addition.
The BitString to add to the current BitString.
A new BitString representing the sum of the two BitStrings.
Performs a bitwise AND operation between this BitString and another BitString or number.
If the input is a number, it is first converted to a BitString. The lengths of both BitStrings are adjusted to match the longer one by padding with zeros if necessary. The result is a new BitString where each bit is the logical AND of the corresponding bits in the two operands.
The BitString or number to perform the AND operation with.
A new BitString representing the result of the bitwise AND operation.
Checks if any bit in the binary string is set to '1'.
true
if at least one bit is '1'; otherwise, false
.
Creates and returns a new BitString
instance that is a copy of the current object.
A new BitString
object with the same binary string as the original.
Determines whether this BitString is equal to another BitString, ignoring leading zeros in their binary string representations.
The BitString instance to compare with.
true
if the normalized binary strings are equal; otherwise, false
.
Retrieves the value of the bit at the specified offset from the end of the bit string.
The 1-based position from the end of the bit string to retrieve the bit from. Must be greater than or equal to 1.
The value to return if the offset is out of bounds. Defaults to false
.
true
if the bit at the specified offset is '1', false
if it is '0', or defaultVal
if the offset is out of bounds.
Performs a left rotation (circular shift) of the bit string by the specified number of positions.
The number of positions to rotate the bit string to the left.
The total number of bits to consider for the rotation. Defaults to 64.
A new BitString
instance with the bits rotated to the left by n
positions.
Shifts the current bit string to the left by the specified number of positions.
This operation appends n
zeros to the right end of the bit string.
The number of positions to shift left (number of zeros to append).
A new BitString
instance with the bits shifted left by n
positions.
Returns a new BitString instance with each bit inverted.
Iterates through the current binary string and flips each bit:
A new BitString with all bits inverted.
Performs a bitwise OR operation between this BitString and another BitString or number.
If a number is provided, it is first converted to a BitString. The operation is performed bit by bit, starting from the least significant bit. The resulting BitString will have a length equal to the longer of the two operands.
The BitString or number to OR with this BitString.
A new BitString representing the result of the bitwise OR operation.
Returns a new BitString
instance with the bits in reverse order.
Optionally sets the length of the bit string before reflecting.
Optional
bits: numberOptional. If provided, sets the length of the bit string before reflecting.
A new BitString
with the bits reversed.
Rotates the bits of the current BitString instance to the right by the specified number of positions.
The number of positions to rotate the bits to the right.
The total number of bits to consider for the rotation. Defaults to 64.
The BitString instance after performing the right rotation.
Performs a right shift operation on the current BitString by the specified number of bits. Pads the left side with zeros and truncates bits from the right.
The number of bits to shift to the right.
A new BitString instance representing the shifted value.
Sets the bit at the specified offset to the given boolean value.
The offset is 1-based, where 1 refers to the least significant bit (rightmost). If the offset exceeds the current length, the bit string is left-padded with zeros as needed.
The 1-based position of the bit to set. Must be greater than or equal to 1.
The boolean value to set the bit to (true
for '1', false
for '0').
The updated BitString instance.
Sets the length of the bit string to the specified number of bits.
If the current length is greater than the specified length, the bit string is truncated from the left. If the current length is less than the specified length, the bit string is left-padded with zeros.
The desired length of the bit string in bits.
The current instance for method chaining.
Reverses the order of bytes in the current bit string.
The number of bytes to consider for swapping. Defaults to 8.
A new BitString
instance with the byte order reversed.
Converts the bit string to an array of bytes.
Optional
bits: numberOptional. If provided, sets the length of the bit string to the specified number of bits before conversion.
An array of numbers, where each number represents a byte from the bit string.
Converts the internal binary string representation (binStr
) to a number.
The numeric value of the binary string.
Converts the bit string to its string representation in the specified format.
The output format for the bit string. Can be BitStringType.BIN
for binary or BitStringType.HEX
for hexadecimal. Defaults to BitStringType.HEX
.
If true
, leading zeros are trimmed from the hexadecimal output. Defaults to false
.
The string representation of the bit string in the specified format. Returns "0"
if the bit string is empty or consists only of zeros.
Performs a bitwise XOR operation between this BitString and another BitString or number.
If the input value is a number, it is first converted to a BitString. The lengths of both BitStrings are adjusted to match by padding with leading zeros if necessary. Returns a new BitString representing the result of the XOR operation.
The BitString or number to XOR with this BitString.
A new BitString containing the result of the XOR operation.
Static
FromCreates a new BitString
instance from one or more byte values.
Each byte is converted to its 8-bit binary representation and concatenated to form the complete bit string.
One or more numbers representing bytes (0-255) to convert.
A new BitString
instance representing the concatenated bits of the input bytes.
Static
Initialize2dInitializes a 2D array of BitString
instances with the specified dimensions.
The number of rows in the 2D array.
The number of columns in each row of the 2D array.
A 2D array (BitString[][]
) where each element is a new instance of BitString
.
Represents a string of bits and provides various bitwise operations and utilities.
The
BitString
class allows for the creation, manipulation, and conversion of bit strings. It supports initialization from numbers, strings (binary or hexadecimal), and provides methods for bitwise operations (AND, OR, XOR, NOT), shifting, rotating, reflecting, and conversion to numbers or byte arrays.Example
Remarks
Version
1.0.0
Since
1.0.5