BitWriter

public protocol BitWriter

A type that contains functions for writing Data bit-by-bit and byte-by-byte.

  • Data which contains the writer’s output (the last byte, that is currently being written, is not included).

    Declaration

    Swift

    var data: Data { get }
  • True, if a bit pointer is aligned to a byte boundary.

    Declaration

    Swift

    var isAligned: Bool { get }
  • Creates an instance for writing bits and bytes.

    Declaration

    Swift

    init()
  • Writes a bit, advancing by one bit position.

    Declaration

    Swift

    func write(bit: UInt8)
  • write(bits:) Default implementation

    Writes bits, advancing by bits.count bit positions.

    Default Implementation

    Writes bits, advancing by bits.count bit positions, using the write(bit:) function.

    Declaration

    Swift

    func write(bits: [UInt8])
  • write(number:bitsCount:) Default implementation

    Writes a number into bitsCount amount of bits, advancing by bitsCount bit positions.

    Default Implementation

    Converts a number into an UInt integer, and writes it into bitsCount amount of bits by using the write(unsignedNumber:bitsCount:) function, advancing by bitsCount bit positions.

    Note

    If the data is supposed to represent a signed integer (i.e. it is important to preserve the sign), it is recommended to use the write(signedNumber:bitsCount:representation:) function.

    Precondition

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

    Declaration

    Swift

    func write(number: Int, bitsCount: Int)
  • Writes a signed integer number into bitsCount amount of bits, advancing by bitsCount bit positions, while using a representation as a method to represent the signed integer in a binary format.

    Default Implementation

    Writes a signed integer number into bitsCount amount of bits, advancing by bitsCount bit positions, while using a representation as a method to represent the signed integer in a binary format. This implementation uses the write(unsignedNumber:bitsCount:) function in the final stage of writing.

    The default value of representation is SignedNumberRepresentation.twoComplementNegatives.

    For the representations where zero can be represented in two ways (namely, .signMagnitude and .oneComplementNegatives), zero is encoded as +0 (i.e. all bits are set to zero).

    Precondition

    The signedNumber must be representable within bitsCount bits using the representation, i.e. it must be in the representation.minRepresentableNumber...representation.maxRepresentableNumber range.

    Precondition

    For the SignedNumberRepresentation.biased representation, the bias must be non-negative.

    Precondition

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

    Declaration

    Swift

    func write(signedNumber: Int, bitsCount: Int, representation: SignedNumberRepresentation)
  • Writes an unsigned number, advancing by bitsCount bit positions.

    Declaration

    Swift

    func write(unsignedNumber: UInt, bitsCount: Int)
  • Writes a byte, advancing by one byte position.

    Declaration

    Swift

    func append(byte: UInt8)
  • Aligns a bit pointer to a byte boundary, i.e. moves the bit pointer to the first bit of the next byte, filling all skipped bit positions with zeros.

    Declaration

    Swift

    func align()