Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions stdlib/public/core/Bit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ public func < (lhs: Bit, rhs: Bit) -> Bool {
}

extension Bit : IntegerArithmeticType {
static func _withOverflow(v: (Int, overflow: Bool)) -> (Bit, overflow: Bool) {
if let b = Bit(rawValue: v.0) {
return (b, v.overflow)
static func _withOverflow(
intResult: Int, overflow: Bool
) -> (Bit, overflow: Bool) {
if let bit = Bit(rawValue: intResult) {
return (bit, overflow: overflow)
} else {
return (Bit(rawValue: v.0 % 2)!, true)
let bitRaw = intResult % 2 + (intResult < 0 ? 2 : 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be simpler, and yet produce the same result: let bitRaw = (intResult + 2) % 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that would rely on the precondition that intResult >= -2. This is true for all current calls to the function, but the way it's written now trivially eliminates this precondition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

let bit = Bit(rawValue: bitRaw)!
return (bit, overflow: true)
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/1_stdlib/Bit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ print(one.predecessor().rawValue)

// CHECK-NEXT: 0
print((one &+ one).rawValue)
// CHECK-NEXT: 1
print((zero &- one).rawValue)

// CHECK: done.
print("done.")
Expand Down