Skip to content

Instantly share code, notes, and snippets.

@gauravkeshre
Last active January 13, 2019 19:52
Show Gist options
  • Save gauravkeshre/8f7eed589926ec80e60fb1b80feb55b4 to your computer and use it in GitHub Desktop.
Save gauravkeshre/8f7eed589926ec80e60fb1b80feb55b4 to your computer and use it in GitHub Desktop.
# Binary representation of a FixedWidthInteger with padding zeros.

Binary representation of a FixedWidthInteger with padding zeros

An initializer in String class String(5, radix: 2) creates a string which is a binary representation of 5 as : 101 But it will not give a padded version (for obvious and good reasons) like 00000101 or 00000000 00000101 which may be desirable in some cases

Here are two approaches of achieving this :

1.

extension String {
    static func binaryRepresentation<F: FixedWidthInteger>(of val: F) -> String {
        let binaryString = String(val, radix: 2)
        if val.leadingZeroBitCount > 0 {
            return String(repeating: "0", count: val.leadingZeroBitCount) + binaryString
        }
        return binaryString
    }
}

// Usage
let five = UInt8(5) // 101
print(String.binaryRepresentation(of: UInt8(5))) //prints 00000101

2

By: Karwag @ SO

extension String {
  init<B: FixedWidthInteger>(fullBinary value: B) {
    self = value.words.reduce(into: "") {
      $0.append(contentsOf: repeatElement("0", count: $1.leadingZeroBitCount))
      $0.append(String($1, radix: 2))
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment