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.count
bit positions.Default Implementation
Writes
bits
, advancing bybits.count
bit positions, using thewrite(bit:)
function.Declaration
Swift
func write(bits: [UInt8])
-
write(number:
Default implementationbitsCount: ) Writes a
number
intobitsCount
amount of bits, advancing bybitsCount
bit positions.Default Implementation
Converts a
number
into anUInt
integer, and writes it intobitsCount
amount of bits by using thewrite(unsignedNumber:bitsCount:)
function, advancing bybitsCount
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 thewrite(signedNumber:bitsCount:representation:)
function.Precondition
ParameterbitsCount
must be in the0...Int.bitWidth
range.Declaration
Swift
func write(number: Int, bitsCount: Int)
-
write(signedNumber:
Default implementationbitsCount: representation: ) Writes a signed integer
number
intobitsCount
amount of bits, advancing bybitsCount
bit positions, while using arepresentation
as a method to represent the signed integer in a binary format.Default Implementation
Writes a signed integer
number
intobitsCount
amount of bits, advancing bybitsCount
bit positions, while using arepresentation
as 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
representation
isSignedNumberRepresentation.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
ThesignedNumber
must be representable withinbitsCount
bits using therepresentation
, i.e. it must be in therepresentation.minRepresentableNumber...representation.maxRepresentableNumber
range.Precondition
For theSignedNumberRepresentation.biased
representation, thebias
must be non-negative.Precondition
ParameterbitsCount
must be in the0...Int.bitWidth
range.Declaration
Swift
func write(signedNumber: Int, bitsCount: Int, representation: SignedNumberRepresentation)
-
Writes an unsigned
number
, advancing bybitsCount
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()