Skip to content

Instantly share code, notes, and snippets.

@nikitamounier
Created December 21, 2021 23:38
Show Gist options
  • Save nikitamounier/70a7d33b5f70be9d98db8b469c4609c1 to your computer and use it in GitHub Desktop.
Save nikitamounier/70a7d33b5f70be9d98db8b469c4609c1 to your computer and use it in GitHub Desktop.
More efficient way of detecting parity
extension BinaryInteger {
@inlinable
var isOdd: Bool {
return withUnsafePointer(to: self & 1) { pointer in
return UnsafeRawPointer(pointer)
.bindMemory(to: Bool.self, capacity: 1)
.pointee
}
}
@inlinable
var isEven: Bool {
return !isOdd
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment