Skip to content

Commit cdfa9ae

Browse files
committed
Adding methods for writing unsigned Int so that writing a UInt64 is possible
1 parent c95d6c0 commit cdfa9ae

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

Sources/BitWriter.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public protocol BitWriter {
2525

2626
/// Writes `number`, using and advancing by `bitsCount` BIT positions.
2727
func write(number: Int, bitsCount: Int)
28+
29+
/// because you cant convert a UInt64 into an Int without potential for overflowing it, we need an unsingned variant of `write`
30+
/// Writes unsigned `number`, using and advancing by `bitsCount` BIT positions.
31+
func write(unsignedNumber: UInt, bitsCount: Int)
2832

2933
/// Writes `byte`, advancing by one BYTE position.
3034
func append(byte: UInt8)

Sources/LsbBitWriter.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,22 @@ public final class LsbBitWriter: BitWriter {
5454
mask <<= 1
5555
}
5656
}
57+
58+
/**
59+
Writes `number`, using and advancing by `bitsCount` BIT positions.
5760

61+
- Note: If `bitsCount` is smaller than the actual amount of `number`'s bits than the `number` will be truncated to
62+
fit into `bitsCount` amount of bits.
63+
- Note: Bits of `number` are processed using the same bit-numbering scheme as of the writer (i.e. "LSB 0").
64+
- Note: this method is specifically useful when needing to write a UInt64 which can overflow and crash if converting to an Int when using the regular `write` method
65+
*/
66+
public func write(unsignedNumber: UInt, bitsCount: Int) {
67+
var mask: UInt = 1
68+
for _ in 0..<bitsCount {
69+
self.write(bit: unsignedNumber & mask > 0 ? 1 : 0)
70+
mask <<= 1
71+
}
72+
}
5873
/**
5974
Writes `byte`, advancing by one BYTE position.
6075

Sources/MsbBitWriter.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ public final class MsbBitWriter: BitWriter {
5454
mask >>= 1
5555
}
5656
}
57+
58+
/**
59+
Writes `number`, using and advancing by `bitsCount` BIT positions.
60+
61+
- Note: If `bitsCount` is smaller than the actual amount of `number`'s bits than the `number` will be truncated to
62+
fit into `bitsCount` amount of bits.
63+
- Note: Bits of `number` are processed using the same bit-numbering scheme as of the writer (i.e. "MSB 0").
64+
- Note: this method is specifically useful when needing to write a UInt64 which can overflow and crash if converting to an Int when using the regular `write` method
65+
*/
66+
public func write(unsignedNumber: UInt, bitsCount: Int) {
67+
var mask: UInt = 1 << (UInt(bitsCount) - 1)
68+
for _ in 0..<bitsCount {
69+
self.write(bit: unsignedNumber & mask > 0 ? 1 : 0)
70+
mask >>= 1
71+
}
72+
}
5773

5874
/**
5975
Writes `byte`, advancing by one BYTE position.

0 commit comments

Comments
 (0)