Skip to content

Instantly share code, notes, and snippets.

@numist
Created September 28, 2021 18:07
Show Gist options
  • Save numist/dfbbc59a881301190a66bb73edbaa402 to your computer and use it in GitHub Desktop.
Save numist/dfbbc59a881301190a66bb73edbaa402 to your computer and use it in GitHub Desktop.
Swift Jigs
extension FixedWidthInteger where Self: UnsignedInteger {
// Enumerates `self`'s bits from most significant to least
var bits: some Sequence {
return sequence(state: Self.bitWidth) { i -> Bool? in
guard i > 0 else { return nil }
i -= 1
return (self & (1 << i) != 0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment