MsbBitReader

public final class MsbBitReader : BitReader

A type that contains functions for reading Data bit-by-bit using “MSB0” bit numbering scheme and byte-by-byte in the Little Endian order.

  • Size of the data (in bytes).

    Declaration

    Swift

    public let size: Int
  • Data which is being read.

    Declaration

    Swift

    public let data: Data
  • Offset to a byte in the data which will be read next.

    Precondition

    The reader must be aligned when accessing the setter of offset.

    Declaration

    Swift

    public var offset: Int { get set }
  • True, if a bit pointer is aligned to a byte boundary.

    Declaration

    Swift

    public var isAligned: Bool { get }
  • Amount of bits left to read.

    Declaration

    Swift

    public var bitsLeft: Int { get }
  • Amount of bits that were already read.

    Declaration

    Swift

    public var bitsRead: Int { get }
  • Creates an instance for reading bits (and bytes) from the data.

    Declaration

    Swift

    public init(data: Data)
  • Converts a ByteReader instance into a LsbBitReader, enabling bit reading capabilities. The current offset value of the byteReader is preserved.

    Declaration

    Swift

    public convenience init(_ byteReader: ByteReader)

Bit reading methods

  • Advances a bit pointer by the specified amount of bits (the default value is 1).

    Warning

    This function doesn’t check if there is any data left. It is advised to use isFinished after calling this method to check if the end was reached.

    Declaration

    Swift

    public func advance(by count: Int = 1)
  • Reads a bit and returns it, advancing by one bit position.

    Precondition

    There must be enough data left.

    Declaration

    Swift

    public func bit() -> UInt8
  • Reads count bits and returns them as a [UInt8] array, advancing by count bit positions.

    Precondition

    Parameter count must non-negative

    Precondition

    There must be enough data left.

    Declaration

    Swift

    public func bits(count: Int) -> [UInt8]
  • Reads fromBits bits, treating them as a binary represenation of a signed integer, and returns the result as a Int number, advancing by fromBits bit positions.

    If the representation doesn’t match the representation that was used to produce the data then the result may be incorrect.

    The default value of representation is SignedNumberRepresentation.twoComplementNegatives.

    Precondition

    Parameter fromBits must be in the 0...Int.bitWidth range.

    Precondition

    There must be enough data left.

    Declaration

    Swift

    public func signedInt(fromBits count: Int, representation: SignedNumberRepresentation = .twoComplementNegatives) -> Int
  • Reads fromBits bits and returns them as a UInt8 number, advancing by fromBits bit positions.

    Precondition

    Parameter fromBits must be in the 0...8 range.

    Precondition

    There must be enough data left.

    Declaration

    Swift

    public func byte(fromBits count: Int) -> UInt8
  • Reads fromBits bits and returns them as a UInt16 number, advancing by fromBits bit positions.

    Precondition

    Parameter fromBits must be in the 0...16 range.

    Precondition

    There must be enough data left.

    Declaration

    Swift

    public func uint16(fromBits count: Int) -> UInt16
  • Reads fromBits bits and returns them as a UInt32 number, advancing by fromBits bit positions.

    Precondition

    Parameter fromBits must be in the 0...32 range.

    Precondition

    There must be enough data left.

    Declaration

    Swift

    public func uint32(fromBits count: Int) -> UInt32
  • Reads fromBits bits and returns them as a UInt64 number, advancing by fromBits bit positions.

    Precondition

    Parameter fromBits must be from 0...64 range.

    Precondition

    There must be enough data left.

    Declaration

    Swift

    public func uint64(fromBits count: Int) -> UInt64
  • Aligns a bit pointer to a byte boundary, i.e. moves the bit pointer to the first bit of the next byte. If the reader is already aligned, then does nothing.

    Warning

    This function doesn’t check if there is any data left. It is advised to use isFinished after calling this method to check if the end was reached.

    Declaration

    Swift

    public func align()

Byte reading methods

  • Reads a byte and returns it, advancing by one byte position.

    Precondition

    The reader must be aligned.

    Precondition

    There must be enough bytes left.

    Declaration

    Swift

    public func byte() -> UInt8
  • Reads count bytes and returns them as a [UInt8] array, advancing by count byte positions.

    Precondition

    The reader must be aligned.

    Precondition

    Parameter count must be non-negative.

    Precondition

    There must be enough bytes left.

    Declaration

    Swift

    public func bytes(count: Int) -> [UInt8]
  • Reads 8 bytes and returns them as a UInt64 number, advancing by 8 byte positions.

    Precondition

    The reader must be aligned.

    Precondition

    There must be enough bytes left.

    Declaration

    Swift

    public func uint64() -> UInt64
  • Reads fromBytes bytes and returns them as a UInt64 number, advancing by fromBytes byte positions.

    Note

    If it is known that the fromBytes is exactly 8 then consider using the uint64() function (without an argument), since it may provide better performance.

    Precondition

    The reader must be aligned.

    Precondition

    Parameter fromBytes must be in the 0...8 range.

    Precondition

    There must be enough bytes left.

    Declaration

    Swift

    public func uint64(fromBytes count: Int) -> UInt64
  • Reads 4 bytes and returns them as a UInt32 number, advancing by 4 byte positions.

    Precondition

    The reader must be aligned.

    Precondition

    There must be enough bytes left.

    Declaration

    Swift

    public func uint32() -> UInt32
  • Reads fromBytes bytes and returns them as a UInt32 number, advancing by fromBytes byte positions.

    Note

    If it is known that the fromBytes is exactly 4 then consider using the uint32() function (without an argument), since it may provide better performance.

    Precondition

    The reader must be aligned.

    Precondition

    Parameter fromBytes must be in the 0...4 range.

    Precondition

    There must be enough bytes left.

    Declaration

    Swift

    public func uint32(fromBytes count: Int) -> UInt32
  • Reads 2 bytes and returns them as a UInt16 number, advancing by 2 byte positions.

    Precondition

    The reader must be aligned.

    Precondition

    There must be enough data left.

    Declaration

    Swift

    public func uint16() -> UInt16
  • Reads fromBytes bytes and returns them as a UInt16 number, advancing by fromBytes byte positions.

    Note

    If it is known that the fromBytes is exactly 2 then consider using the uint16() function (without an argument), since it may provide better performance.

    Precondition

    The reader must be aligned.

    Precondition

    Parameter fromBytes must be in the 0...2 range.

    Precondition

    There must be enough bytes left.

    Declaration

    Swift

    public func uint16(fromBytes count: Int) -> UInt16