Skip to content

Commit c486a3a

Browse files
committed
Merge pull request #680 from PatrickPijnappel/bit-fix
[stdlib] Fix bug in Bit with-overflow arithmetic
2 parents 1f95076 + f819683 commit c486a3a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

stdlib/public/core/Bit.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,15 @@ public func < (lhs: Bit, rhs: Bit) -> Bool {
9494
}
9595

9696
extension Bit : IntegerArithmeticType {
97-
static func _withOverflow(v: (Int, overflow: Bool)) -> (Bit, overflow: Bool) {
98-
if let b = Bit(rawValue: v.0) {
99-
return (b, v.overflow)
97+
static func _withOverflow(
98+
intResult: Int, overflow: Bool
99+
) -> (Bit, overflow: Bool) {
100+
if let bit = Bit(rawValue: intResult) {
101+
return (bit, overflow: overflow)
100102
} else {
101-
return (Bit(rawValue: v.0 % 2)!, true)
103+
let bitRaw = intResult % 2 + (intResult < 0 ? 2 : 0)
104+
let bit = Bit(rawValue: bitRaw)!
105+
return (bit, overflow: true)
102106
}
103107
}
104108

test/1_stdlib/Bit.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ print(one.predecessor().rawValue)
2727

2828
// CHECK-NEXT: 0
2929
print((one &+ one).rawValue)
30+
// CHECK-NEXT: 1
31+
print((zero &- one).rawValue)
3032

3133
// CHECK: done.
3234
print("done.")

0 commit comments

Comments
 (0)