Skip to content

Commit 9f16272

Browse files
[stdlib] Fix bug in Bit with-overflow arithmetic
The original version would result in a crash for negative results.
1 parent 3bc99c7 commit 9f16272

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

stdlib/public/core/Bit.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ extension Bit : IntegerArithmeticType {
9898
if let b = Bit(rawValue: v.0) {
9999
return (b, v.overflow)
100100
} else {
101-
return (Bit(rawValue: v.0 % 2)!, true)
101+
let bitRaw = v.0 > 0 ? v.0 % 2 : v.0 % 2 + 2
102+
return (Bit(rawValue: bitRaw)!, true)
102103
}
103104
}
104105

0 commit comments

Comments
 (0)