Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A generic function to print the "binary string" of any `FixedWidthInteger`.
func print<T: FixedWidthInteger>(asBinary val: T) {
let bitCount = MemoryLayout<T>.size * 8
let binaryStr = String(val, radix: 2)
let zeroPadding = String(repeating: "0", count: bitCount - binaryStr.count)
print("0b\(zeroPadding)\(binaryStr)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.