diff --git a/Sources/BitCollections/BitArray/BinaryInteger extensions.swift b/Sources/BitCollections/BitArray/BinaryInteger extensions.swift index 1e1ae26d..291aafb7 100644 --- a/Sources/BitCollections/BitArray/BinaryInteger extensions.swift +++ b/Sources/BitCollections/BitArray/BinaryInteger extensions.swift @@ -23,7 +23,9 @@ extension BitArray { value: T, isNegative: Bool ) { - var value: T = .zero + // Negative values start out with all bits set, so that they remain sign + // extended in types that are wider than the words we shift in. + var value: T = T.isSigned && self.last == true ? ~.zero : .zero let isNegative = self._foreachTwosComplementWordDownward( isSigned: T.isSigned ) { _, word in @@ -62,7 +64,7 @@ extension BitArray { package func _convertForced(to type: T.Type) -> T { guard let value = _convertExactly(to: T.self) else { fatalError(""" - BitArray value cannot be converted to \(Self.self) because it is \ + BitArray value cannot be converted to \(T.self) because it is \ outside the representable range """) } diff --git a/Tests/BitCollectionsTests/BitArrayTests.swift b/Tests/BitCollectionsTests/BitArrayTests.swift index 0f7f8ef5..001ed7fe 100644 --- a/Tests/BitCollectionsTests/BitArrayTests.swift +++ b/Tests/BitCollectionsTests/BitArrayTests.swift @@ -259,6 +259,54 @@ final class BitArrayTests: CollectionTestCase { } } + func test_conversion_to_wide_BinaryInteger() { + guard #available(SwiftStdlib 6.0, *) else { return } + func bits(_ s: String) -> BitArray { BitArray(s)! } + func ones(_ n: Int) -> String { String(repeating: "1", count: n) } + func zeros(_ n: Int) -> String { String(repeating: "0", count: n) } + + let cases: [(bits: BitArray, signed: Int128, unsigned: UInt128)] = [ + ("", 0, 0), + ("1", -1, 1), + ("01", 1, 1), + ("10", -2, 2), + ("101", -3, 5), + (bits(ones(64)), -1, UInt128(UInt64.max)), + (bits("1" + zeros(63)), -1 << 63, 1 << 63), + (bits("0" + ones(64)), Int128(UInt64.max), UInt128(UInt64.max)), + (bits("1" + zeros(64)), -1 << 64, 1 << 64), + (bits(ones(65)), -1, (1 << 65) - 1), + (bits("1" + zeros(127)), .min, 1 << 127), + (bits("0" + ones(127)), .max, UInt128(Int128.max)), + (bits(ones(128)), -1, .max), + (bits("0" + ones(128)), -1, .max), + (bits("11" + zeros(128)), 0, 0), + ] + withEvery("pair", in: cases) { (bits, signed, unsigned) in + expectEqual(Int128(truncatingIfNeeded: bits), signed) + expectEqual(UInt128(truncatingIfNeeded: bits), unsigned) + } + + let exactCases: [(bits: BitArray, signed: Int128?, unsigned: UInt128?)] = [ + ("", 0, 0), + ("1", -1, 1), + ("10", -2, 2), + (bits(ones(64)), -1, UInt128(UInt64.max)), + (bits("1" + zeros(64)), -1 << 64, 1 << 64), + (bits("1" + zeros(127)), .min, 1 << 127), + (bits(ones(128)), -1, .max), + (bits(ones(200)), -1, nil), + (bits("0" + ones(128)), nil, .max), + (bits("10" + zeros(127)), nil, nil), + ] + withEvery("pair", in: exactCases) { (bits, signed, unsigned) in + expectEqual(Int128(exactly: bits), signed) + expectEqual(UInt128(exactly: bits), unsigned) + if let signed { expectEqual(Int128(bits), signed) } + if let unsigned { expectEqual(UInt128(bits), unsigned) } + } + } + func test_init_BitSet() { expectEqualElements(BitArray(BitSet([])), []) expectEqualElements(BitArray(BitSet([0])), [true])