Skip to content

Commit e0a2844

Browse files
authored
Merge pull request #1 from cowgp/develop
Add methods for writing unsigned Int so it's possible to write a UInt64
2 parents c95d6c0 + 14e6b86 commit e0a2844

5 files changed

Lines changed: 48 additions & 0 deletions

File tree

Sources/BitWriter.swift

100644100755
File mode changed.

Sources/LsbBitWriter.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,23 @@ 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+
}
73+
5874
/**
5975
Writes `byte`, advancing by one BYTE position.
6076

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(truncatingIfNeeded: 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.

Tests/BitByteDataTests/LsbBitWriterTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,13 @@ class LsbBitWriterTests: XCTestCase {
7878
let bitReader = LsbBitReader(data: bitWriter.data)
7979
XCTAssertEqual(bitReader.int(fromBits: 14), 14582)
8080
}
81+
82+
func testUInt64() {
83+
let bitWriter = LsbBitWriter()
84+
bitWriter.write(unsignedNumber: UInt(UInt64.max), bitsCount: UInt64.bitWidth)
85+
86+
let byteReader = ByteReader.init(data: bitWriter.data)
87+
XCTAssertEqual(byteReader.uint64(), UInt64.max)
88+
}
8189

8290
}

Tests/BitByteDataTests/MsbBitWriterTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,13 @@ class MsbBitWriterTests: XCTestCase {
8181
let bitReader = MsbBitReader(data: bitWriter.data)
8282
XCTAssertEqual(bitReader.int(fromBits: 14), 14582)
8383
}
84+
85+
func testUInt64() {
86+
let bitWriter = MsbBitWriter()
87+
bitWriter.write(unsignedNumber: UInt(UInt64.max), bitsCount: UInt64.bitWidth)
88+
89+
let byteReader = ByteReader.init(data: bitWriter.data)
90+
XCTAssertEqual(byteReader.uint64(), UInt64.max)
91+
}
8492

8593
}

0 commit comments

Comments
 (0)