BitVector

sealed abstract class BitVector extends BitwiseOperations[BitVector, Long] with Ordered[BitVector] with Serializable

Persistent vector of bits, stored as bytes.

Persistent vector of bits, stored as bytes.

Bits are numbered left to right, starting at 0.

Companion
object
trait Serializable
trait Ordered[BitVector]
trait Comparable[BitVector]
class Object
trait Matchable
class Any

Document{}

def size: Long

Returns number of bits in this vector.

Returns number of bits in this vector.

final def length: Long

Alias for size.

Alias for size.

final def isEmpty: Boolean

Returns true if this vector has no bits.

Returns true if this vector has no bits.

final def nonEmpty: Boolean

Returns true if this vector has a non-zero number of bits.

Returns true if this vector has a non-zero number of bits.

final def sizeGreaterThan(n: Long): Boolean

Returns true if the size of this BitVector is greater than n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.

Returns true if the size of this BitVector is greater than n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.

final def sizeGreaterThanOrEqual(n: Long): Boolean

Returns true if the size of this BitVector is greater than or equal to n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.

Returns true if the size of this BitVector is greater than or equal to n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.

def sizeLessThan(n: Long): Boolean

Returns true if the size of this BitVector is less than n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.

Returns true if the size of this BitVector is less than n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.

final def sizeLessThanOrEqual(n: Long): Boolean

Returns true if the size of this BitVector is less than or equal to n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.

Returns true if the size of this BitVector is less than or equal to n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.

final def intSize: Option[Int]

Returns the number of bits in this vector, or None if the size does not fit into an Int.

Returns the number of bits in this vector, or None if the size does not fit into an Int.

def get(n: Long): Boolean

Returns true if the nth bit is high, false otherwise.

Returns true if the nth bit is high, false otherwise.

Throws
NoSuchElementException

if n >= size

def getByte(n: Long): Byte

Returns the nth byte, 0-indexed.

Returns the nth byte, 0-indexed.

Throws
NoSuchElementException

if n >= bytes.size

final def apply(n: Long): Boolean

Alias for get.

Alias for get.

See also

get(Long)

final def lift(n: Long): Option[Boolean]

Returns Some(true) if the nth bit is high, Some(false) if low, and None if n >= size.

Returns Some(true) if the nth bit is high, Some(false) if low, and None if n >= size.

def update(n: Long, high: Boolean): BitVector

Returns a new bit vector with the nth bit high if high is true or low if high is false.

Returns a new bit vector with the nth bit high if high is true or low if high is false.

final def insert(idx: Long, b: Boolean): BitVector

Returns a vector with the specified bit inserted at the specified index.

Returns a vector with the specified bit inserted at the specified index.

final def splice(idx: Long, b: BitVector): BitVector

Returns a vector with the specified bit vector inserted at the specified index.

Returns a vector with the specified bit vector inserted at the specified index.

final def patch(idx: Long, b: BitVector): BitVector

Returns a vector with the specified bit vector replacing bits [idx, idx + b.size].

Returns a vector with the specified bit vector replacing bits [idx, idx + b.size].

final def set(n: Long): BitVector

Returns a new bit vector with the nth bit high (and all other bits unmodified).

Returns a new bit vector with the nth bit high (and all other bits unmodified).

final def clear(n: Long): BitVector

Returns a new bit vector with the nth bit low (and all other bits unmodified).

Returns a new bit vector with the nth bit low (and all other bits unmodified).

Returns a new bit vector representing this vector's contents followed by the specified vector's contents.

Returns a new bit vector representing this vector's contents followed by the specified vector's contents.

final def +:(b: Boolean): BitVector

Returns a new vector with the specified bit prepended.

Returns a new vector with the specified bit prepended.

final def :+(b: Boolean): BitVector

Returns a new vector with the specified bit appended.

Returns a new vector with the specified bit appended.

def drop(n: Long): BitVector

Returns a vector of all bits in this vector except the first n bits.

Returns a vector of all bits in this vector except the first n bits.

The resulting vector's size is 0 max (size - n).

final def dropRight(n: Long): BitVector

Returns a vector of all bits in this vector except the last n bits.

Returns a vector of all bits in this vector except the last n bits.

The resulting vector's size is 0 max (size - n).

def take(n: Long): BitVector

Returns a vector of the first n bits of this vector.

Returns a vector of the first n bits of this vector.

The resulting vector's size is n min size.

Note: if an n-bit vector is required, use the acquire method instead.

See also

acquire

final def takeRight(n: Long): BitVector

Returns a vector of the last n bits of this vector.

Returns a vector of the last n bits of this vector.

The resulting vector's size is n min size.

final def splitAt(n: Long): (BitVector, BitVector)

Returns a pair of vectors that is equal to (take(n), drop(n)).

Returns a pair of vectors that is equal to (take(n), drop(n)).

final def slice(from: Long, until: Long): BitVector

Returns a vector made up of the bits starting at index from up to index until, not including the index until.

Returns a vector made up of the bits starting at index from up to index until, not including the index until.

def acquire(n: Long): Either[String, BitVector]

Returns a vector whose contents are the results of taking the first n bits of this vector.

Returns a vector whose contents are the results of taking the first n bits of this vector.

If this vector does not contain at least n bits, an error message is returned.

See also

take

final def acquireThen[R](n: Long)(err: String => R, f: BitVector => R): R

Like aquire, but immediately consumes the Either via the pair of functions err and f.

Like aquire, but immediately consumes the Either via the pair of functions err and f.

See also

acquire

final def consume[A](n: Long)(decode: BitVector => Either[String, A]): Either[String, (BitVector, A)]

Consumes the first n bits of this vector and decodes them with the specified function, resulting in a vector of the remaining bits and the decoded value. If this vector does not have n bits or an error occurs while decoding, an error is returned instead.

Consumes the first n bits of this vector and decodes them with the specified function, resulting in a vector of the remaining bits and the decoded value. If this vector does not have n bits or an error occurs while decoding, an error is returned instead.

final def consumeThen[R](n: Long)(err: String => R, f: (BitVector, BitVector) => R): R

If this vector has at least n bits, returns f(take(n),drop(n)), otherwise calls err with a meaningful error message. This function can be used to avoid intermediate allocations of Either objects when using acquire or consume directly.

If this vector has at least n bits, returns f(take(n),drop(n)), otherwise calls err with a meaningful error message. This function can be used to avoid intermediate allocations of Either objects when using acquire or consume directly.

See also

acquireThen

final def startsWith(b: BitVector): Boolean

Returns true if this bit vector starts with the specified vector.

Returns true if this bit vector starts with the specified vector.

final def endsWith(b: BitVector): Boolean

Returns true if this bit vector ends with the specified vector.

Returns true if this bit vector ends with the specified vector.

final def indexOfSlice(slice: BitVector): Long

Finds the first index of the specified bit pattern in this vector.

Finds the first index of the specified bit pattern in this vector.

Returns

index of slice or -1 if not found

final def indexOfSlice(slice: BitVector, from: Long): Long

Finds the first index after from of the specified bit pattern in this vector.

Finds the first index after from of the specified bit pattern in this vector.

Returns

index of slice or -1 if not found

final def containsSlice(slice: BitVector): Boolean

Determines if the specified slice is in this vector.

Determines if the specified slice is in this vector.

final def head: Boolean

Returns the first bit of this vector or throws if vector is emtpy.

Returns the first bit of this vector or throws if vector is emtpy.

final def headOption: Option[Boolean]

Returns the first bit of this vector or None if vector is emtpy.

Returns the first bit of this vector or None if vector is emtpy.

final def tail: BitVector

Returns a vector of all bits in this vector except the first bit.

Returns a vector of all bits in this vector except the first bit.

final def init: BitVector

Returns a vector of all bits in this vector except the last bit.

Returns a vector of all bits in this vector except the last bit.

final def last: Boolean

Returns the last bit in this vector or throws if vector is empty.

Returns the last bit in this vector or throws if vector is empty.

final def lastOption: Option[Boolean]

Returns the last bit in this vector or returns None if vector is empty.

Returns the last bit in this vector or returns None if vector is empty.

final def padTo(n: Long): BitVector

Alias for padRight.

Alias for padRight.

Throws
IllegalArgumentException

if n < size

final def padRight(n: Long): BitVector

Returns an n-bit vector whose contents are 0 or more low bits followed by this vector's contents.

Returns an n-bit vector whose contents are 0 or more low bits followed by this vector's contents.

Throws
IllegalArgumentException

if n < size

final def padLeft(n: Long): BitVector

Returns an n-bit vector whose contents are 0 or more low bits followed by this vector's contents.

Returns an n-bit vector whose contents are 0 or more low bits followed by this vector's contents.

Throws
IllegalArgumentException

if n < size

final def reverse: BitVector

Reverse the bits of this vector.

Reverse the bits of this vector.

Returns a new vector of the same size with the byte order reversed.

Returns a new vector of the same size with the byte order reversed.

Note that reverseByteOrder.reverseByteOrder == identity only when size is evenly divisble by 8. To invert reverseByteOrder for an arbitrary size, use invertReverseByteOrder.

Inverse of reverseByteOrder.

Inverse of reverseByteOrder.

Returns a new vector of the same size with the bit order reversed.

Returns a new vector of the same size with the bit order reversed.

final def populationCount: Long

Returns the number of bits that are high.

Returns the number of bits that are high.

final def compact: Bytes

Return a BitVector with the same contents as this, but based off a single ByteVector.

Return a BitVector with the same contents as this, but based off a single ByteVector.

This may involve copying data to a fresh ByteVector, but has the advantage that lookups index directly into a single ByteVector rather than traversing a logarithmic number of nodes in this tree.

Calling this method on an already compacted vector is a no-op.

def align: Bytes

Produce a single flat Bytes by interpreting any non-byte-aligned appends or drops. Unlike compact, the underlying ByteVector is not necessarily copied.

Produce a single flat Bytes by interpreting any non-byte-aligned appends or drops. Unlike compact, the underlying ByteVector is not necessarily copied.

final def copy: Bytes

Return a BitVector with the same contents as this, but based off a single flat ByteVector. This function is guaranteed to copy all the bytes in this BitVector, unlike compact, which may no-op if this BitVector already consists of a single ByteVector chunk.

Return a BitVector with the same contents as this, but based off a single flat ByteVector. This function is guaranteed to copy all the bytes in this BitVector, unlike compact, which may no-op if this BitVector already consists of a single ByteVector chunk.

final def force: BitVector

Forces any Suspend nodes in this BitVector and ensures the tree is balanced.

Forces any Suspend nodes in this BitVector and ensures the tree is balanced.

final def toIndexedSeq: IndexedSeq[Boolean]

Return the sequence of bits in this vector. The returned IndexedSeq is just a view; nothing is actually copied.

Return the sequence of bits in this vector. The returned IndexedSeq is just a view; nothing is actually copied.

Throws
IllegalArgumentException

if this vector's size exceeds Int.MaxValue

See also

acquire

toIndexedSeq

Converts the contents of this vector to a byte vector.

Converts the contents of this vector to a byte vector.

If this vector's size does not divide evenly by 8, the last byte of the returned vector will be zero-padded to the right.

final def bytes: ByteVector

Alias for toByteVector.

Alias for toByteVector.

final def toByteArray: Array[Byte]

Converts the contents of this vector to a byte array.

Converts the contents of this vector to a byte array.

If this vector's size does not divide evenly by 8, the last byte of the returned vector will be zero-padded to the right.

final def toByteBuffer: ByteBuffer

Converts the contents of this vector to a java.nio.ByteBuffer.

Converts the contents of this vector to a java.nio.ByteBuffer.

The returned buffer is freshly allocated with limit set to the minimum number of bytes needed to represent the contents of this vector, position set to zero, and remaining set to the limit.

See also

toByteVector

final def toBin: String

Converts the contents of this bit vector to a binary string of size digits.

Converts the contents of this bit vector to a binary string of size digits.

final def toBin(alphabet: BinaryAlphabet): String

Converts the contents of this bit vector to a binary string of size digits.

Converts the contents of this bit vector to a binary string of size digits.

final def toHex: String

Converts the contents of this bit vector to a hexadecimal string of ceil(size / 4) nibbles.

Converts the contents of this bit vector to a hexadecimal string of ceil(size / 4) nibbles.

The last nibble is right-padded with zeros if the size is not evenly divisible by 4.

final def toHex(alphabet: HexAlphabet): String

Converts the contents of this bit vector to a hexadecimal string of ceil(size / 4) nibbles.

Converts the contents of this bit vector to a hexadecimal string of ceil(size / 4) nibbles.

The last nibble is right-padded with zeros if the size is not evenly divisible by 4.

final def toBase16: String

Helper alias of toHex

Helper alias of toHex

final def toBase16(alphabet: HexAlphabet): String

Helper alias of toHex

Helper alias of toHex

final def toBase32: String

Converts the contents of this vector to a base 32 string.

Converts the contents of this vector to a base 32 string.

The last byte is right-padded with zeros if the size is not evenly divisible by 8.

final def toBase32(alphabet: Base32Alphabet): String

Converts the contents of this vector to a base 32 string using the specified alphabet.

Converts the contents of this vector to a base 32 string using the specified alphabet.

The last byte is right-padded with zeros if the size is not evenly divisible by 8.

final def toBase58: String

Converts the contents of this vector to a base 58 string.

Converts the contents of this vector to a base 58 string.

the order is assumed to be the same as (Bitcoin)https://en.bitcoin.it/wiki/Base58Check_encoding#Base58_symbol_chart

final def toBase58(alphabet: Alphabet): String

Converts the contents of this vector to a base 58 string using the specified alphabet.

Converts the contents of this vector to a base 58 string using the specified alphabet.

the order is assumed to be the same as (Bitcoin)https://en.bitcoin.it/wiki/Base58Check_encoding#Base58_symbol_chart

final def toBase64: String

Converts the contents of this vector to a base 64 string.

Converts the contents of this vector to a base 64 string.

The last byte is right-padded with zeros if the size is not evenly divisible by 8.

final def toBase64(alphabet: Base64Alphabet): String

Converts the contents of this vector to a base 64 string using the specified alphabet.

Converts the contents of this vector to a base 64 string using the specified alphabet.

The last byte is right-padded with zeros if the size is not evenly divisible by 8.

final def toBase64NoPad: String

Converts the contents of this vector to a base 64 string without padding.

Converts the contents of this vector to a base 64 string without padding.

final def toBase64Url: String

Converts the contents of this vector to a base 64 string without padding.

Converts the contents of this vector to a base 64 string without padding.

final def toBase64UrlNoPad: String

Converts the contents of this vector to a base 64 string without padding.

Converts the contents of this vector to a base 64 string without padding.

final def sliceToByte(start: Long, bits: Int, signed: Boolean): Byte

Convert a slice of bits from this vector (start until start+bits) to a Byte.

Convert a slice of bits from this vector (start until start+bits) to a Byte.

Value Params
signed

whether sign extension should be performed

Throws
IllegalArgumentException

if the slice refers to indices that are out of range

final def toByte(signed: Boolean): Byte

Converts the contents of this vector to a byte.

Converts the contents of this vector to a byte.

Value Params
signed

whether sign extension should be performed

Throws
IllegalArgumentException

if size is greater than 8

final def sliceToShort(start: Long, bits: Int, signed: Boolean, ordering: ByteOrdering): Short

Convert a slice of bits from this vector (start until start+bits) to a Short.

Convert a slice of bits from this vector (start until start+bits) to a Short.

Value Params
ordering

order bytes should be processed in

signed

whether sign extension should be performed

Throws
IllegalArgumentException

if the slice refers to indices that are out of range

final def toShort(signed: Boolean, ordering: ByteOrdering): Short

Converts the contents of this vector to a short.

Converts the contents of this vector to a short.

Value Params
ordering

order bytes should be processed in

signed

whether sign extension should be performed

Throws
IllegalArgumentException

if size is greater than 16

final def sliceToInt(start: Long, bits: Int, signed: Boolean, ordering: ByteOrdering): Int

Convert a slice of bits from this vector (start until start+bits) to an Int.

Convert a slice of bits from this vector (start until start+bits) to an Int.

Value Params
ordering

order bytes should be processed in

signed

whether sign extension should be performed

Throws
IllegalArgumentException

if the slice refers to indices that are out of range

final def toInt(signed: Boolean, ordering: ByteOrdering): Int

Converts the contents of this vector to an int.

Converts the contents of this vector to an int.

Value Params
ordering

order bytes should be processed in

signed

whether sign extension should be performed

Throws
IllegalArgumentException

if size is greater than 32

final def sliceToLong(start: Long, bits: Int, signed: Boolean, ordering: ByteOrdering): Long

Convert a slice of bits from this vector (start until start+bits) to a Long.

Convert a slice of bits from this vector (start until start+bits) to a Long.

Value Params
ordering

order bytes should be processed in

signed

whether sign extension should be performed

Throws
IllegalArgumentException

if the slice refers to indices that are out of range

final def toLong(signed: Boolean, ordering: ByteOrdering): Long

Converts the contents of this vector to a long.

Converts the contents of this vector to a long.

Value Params
ordering

order bytes should be processed in

signed

whether sign extension should be performed

Throws
IllegalArgumentException

if size is greater than 64

final def toUUID: UUID

Converts the contents of this bit vector to a UUID.

Converts the contents of this bit vector to a UUID.

Throws
IllegalArgumentException

if size is not exactly 128.

final def decodeString(charset: Charset): Either[CharacterCodingException, String]

Decodes this vector as a string using the implicitly available charset.

Decodes this vector as a string using the implicitly available charset.

final def decodeUtf8: Either[CharacterCodingException, String]

Decodes this vector as a string using the UTF-8 charset.

Decodes this vector as a string using the UTF-8 charset.

final def decodeAscii: Either[CharacterCodingException, String]

Decodes this vector as a string using the US-ASCII charset.

Decodes this vector as a string using the US-ASCII charset.

final def deflate(level: Int, strategy: Int, nowrap: Boolean, chunkSize: Int): BitVector

Compresses this vector using ZLIB.

Compresses this vector using ZLIB.

The last byte is zero padded if the size is not evenly divisible by 8.

Value Params
chunkSize

buffer size, in bytes, to use when compressing

level

compression level, 0-9, with 0 disabling compression and 9 being highest level of compression -- see java.util.zip.Deflater for details

nowrap

if true, ZLIB header and checksum will not be used

strategy

compression strategy -- see java.util.zip.Deflater for details

final def inflate(chunkSize: Int): Either[DataFormatException, BitVector]

Decompresses this vector using ZLIB.

Decompresses this vector using ZLIB.

The last byte is zero padded if the size is not evenly divisible by 8.

Value Params
chunkSize

buffer size, in bytes, to use when compressing

final def digest(algorithm: String): BitVector

Computes a digest of this bit vector.

Computes a digest of this bit vector.

Exceptions thrown from the underlying JCA API are propagated.

The last byte is zero padded if the size is not evenly divisible by 8.

Value Params
algorithm

digest algorithm to use

final def digest(digest: MessageDigest): BitVector

Computes a digest of this bit vector.

Computes a digest of this bit vector.

Exceptions thrown from the underlying JCA API are propagated.

The last byte is zero padded if the size is not evenly divisible by 8.

Value Params
digest

digest to use

final def encrypt(ci: Cipher, key: Key, aparams: Option[AlgorithmParameters])(sr: SecureRandom): Either[GeneralSecurityException, BitVector]

Encrypts this bit vector using the specified cipher and key.

Encrypts this bit vector using the specified cipher and key.

The last byte is zero padded if the size is not evenly divisible by 8.

Value Params
aparams

optional algorithm paramaters used for encryption (e.g., initialization vector)

ci

cipher to use for encryption

key

key to encrypt with

sr

secure random

final def decrypt(ci: Cipher, key: Key, aparams: Option[AlgorithmParameters])(sr: SecureRandom): Either[GeneralSecurityException, BitVector]

Decrypts this bit vector using the specified cipher and key.

Decrypts this bit vector using the specified cipher and key.

The last byte is zero padded if the size is not evenly divisible by 8.

Value Params
aparams

optional algorithm paramaters used for decryption (e.g., initialization vector)

ci

cipher to use for decryption

key

key to decrypt with

sr

secure random

final def ===(other: BitVector): Boolean

Returns true if the specified BitVector has the same contents as this vector.

Returns true if the specified BitVector has the same contents as this vector.

final override def equals(other: Any): Boolean

Returns true if the specified value is a BitVector with the same contents as this vector.

Returns true if the specified value is a BitVector with the same contents as this vector.

See also
Definition Classes
Any
final lazy override val hashCode: Int

Calculates the hash code of this vector. The result is cached.

Calculates the hash code of this vector. The result is cached.

final override def toString: String

Display the size and bytes of this BitVector. For bit vectors beyond a certain size, only a hash of the contents are shown.

Display the size and bytes of this BitVector. For bit vectors beyond a certain size, only a hash of the contents are shown.

Definition Classes
Any
final def ^(other: BitVector): BitVector

Returns a bitwise XOR of this value with the specified value.

Returns a bitwise XOR of this value with the specified value.

The resulting value's size is the minimum of this value's size and the specified value's size.

Inherited from
BitwiseOperations
final def unary_~: BitVector

Returns a bitwise complement of this value.

Returns a bitwise complement of this value.

Inherited from
BitwiseOperations
final def <<(n: Long): BitVector

Returns a value of the same size with each bit shifted to the left n bits.

Returns a value of the same size with each bit shifted to the left n bits.

Inherited from
BitwiseOperations
def iff(other: BitVector): BitVector

Returns a bitwise if-and-only-if of this value with the specified value.

Returns a bitwise if-and-only-if of this value with the specified value.

The resulting value's size is the minimum of this value's size and the specified value's size.

Inherited from
BitwiseOperations
final def >>(n: Long): BitVector

Returns a value of the same size with each bit shifted to the right n bits where the n left-most bits are sign extended.

Returns a value of the same size with each bit shifted to the right n bits where the n left-most bits are sign extended.

Inherited from
BitwiseOperations
final def &(other: BitVector): BitVector

Returns a bitwise AND of this value with the specified value.

Returns a bitwise AND of this value with the specified value.

The resulting value's size is the minimum of this value's size and the specified value's size.

Inherited from
BitwiseOperations
final def |(other: BitVector): BitVector

Returns a bitwise OR of this value with the specified value.

Returns a bitwise OR of this value with the specified value.

The resulting value's size is the minimum of this value's size and the specified value's size.

Inherited from
BitwiseOperations
def nor(other: BitVector): BitVector

Returns a bitwise NOR of this value with the specified value.

Returns a bitwise NOR of this value with the specified value.

The resulting value's size is the minimum of this value's size and the specified value's size.

Inherited from
BitwiseOperations
def nand(other: BitVector): BitVector

Returns a bitwise NAND of this value with the specified value.

Returns a bitwise NAND of this value with the specified value.

The resulting value's size is the minimum of this value's size and the specified value's size.

Inherited from
BitwiseOperations
final def >>>(n: Long): BitVector

Returns a value of the same size with each bit shifted to the right n bits where the n left-most bits are low.

Returns a value of the same size with each bit shifted to the right n bits where the n left-most bits are low.

Inherited from
BitwiseOperations

Returns a bitwise implication of this value with the specified value.

Returns a bitwise implication of this value with the specified value.

The resulting value's size is the minimum of this value's size and the specified value's size.

Inherited from
BitwiseOperations

Value members

Concrete methods

final def and(other: BitVector): BitVector
override def compare(that: BitVector): Int
Definition Classes
Ordered
final def not: BitVector
final def or(other: BitVector): BitVector
final def rotateLeft(n: Long): BitVector
final def rotateRight(n: Long): BitVector
final def shiftLeft(n: Long): BitVector
final def shiftRight(n: Long, signExtension: Boolean): BitVector
final def xor(other: BitVector): BitVector

Inherited methods

def <(that: BitVector): Boolean
Inherited from
Ordered
def <=(that: BitVector): Boolean
Inherited from
Ordered
def >(that: BitVector): Boolean
Inherited from
Ordered
def >=(that: BitVector): Boolean
Inherited from
Ordered
def compareTo(that: BitVector): Int
Inherited from
Ordered