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 bybits.countbit positions.Default Implementation
Writes
bits, advancing bybits.countbit positions, using thewrite(bit:)function.Declaration
Swift
func write(bits: [UInt8]) -
write(number:Default implementationbitsCount: ) Writes a
numberintobitsCountamount of bits, advancing bybitsCountbit positions.Default Implementation
Converts a
numberinto anUIntinteger, and writes it intobitsCountamount of bits by using thewrite(unsignedNumber:bitsCount:)function, advancing bybitsCountbit 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 thewrite(signedNumber:bitsCount:representation:)function.Precondition
ParameterbitsCountmust be in the0...Int.bitWidthrange.Declaration
Swift
func write(number: Int, bitsCount: Int) -
write(signedNumber:Default implementationbitsCount: representation: ) Writes a signed integer
numberintobitsCountamount of bits, advancing bybitsCountbit positions, while using arepresentationas a method to represent the signed integer in a binary format.Default Implementation
Writes a signed integer
numberintobitsCountamount of bits, advancing bybitsCountbit positions, while using arepresentationas a method to represent the signed integer in a binary format. This implementation uses thewrite(unsignedNumber:bitsCount:)function in the final stage of writing.The default value of
representationisSignedNumberRepresentation.twoComplementNegatives.For the representations where zero can be represented in two ways (namely,
.signMagnitudeand.oneComplementNegatives), zero is encoded as +0 (i.e. all bits are set to zero).Precondition
ThesignedNumbermust be representable withinbitsCountbits using therepresentation, i.e. it must be in therepresentation.minRepresentableNumber...representation.maxRepresentableNumberrange.Precondition
For theSignedNumberRepresentation.biasedrepresentation, thebiasmust be non-negative.Precondition
ParameterbitsCountmust be in the0...Int.bitWidthrange.Declaration
Swift
func write(signedNumber: Int, bitsCount: Int, representation: SignedNumberRepresentation) -
Writes an unsigned
number, advancing bybitsCountbit 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()
View on GitHub
BitWriter Protocol Reference