Created
December 21, 2021 23:38
-
-
Save nikitamounier/70a7d33b5f70be9d98db8b469c4609c1 to your computer and use it in GitHub Desktop.
More efficient way of detecting parity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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