Skip to content

Instantly share code, notes, and snippets.

@ctreffs
Created April 9, 2020 07:03
Show Gist options
  • Save ctreffs/1efcb91d3d1460dcbd8bfe35488bbbe7 to your computer and use it in GitHub Desktop.
Save ctreffs/1efcb91d3d1460dcbd8bfe35488bbbe7 to your computer and use it in GitHub Desktop.
extension BinaryInteger {
/// Provides a padded binary representation of this value (i.e. 01001001).
public var binaryDescription: String {
let str = String(self, radix: 2)
return String(repeatElement("0", count: (MemoryLayout<Self>.stride * 8) - str.utf8.count)) + str
}
}
extension OptionSet where RawValue: BinaryInteger {
/// Provides a padded binary representation of this value (i.e. 01001001).
public var binaryDescription: String {
return rawValue.binaryDescription
}
}
extension RawRepresentable where RawValue: BinaryInteger {
/// Provides a padded binary representation of this value (i.e. 01001001).
public var binaryDescription: String {
return rawValue.binaryDescription
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment