-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUInt8+Bit.swift
More file actions
42 lines (34 loc) · 952 Bytes
/
UInt8+Bit.swift
File metadata and controls
42 lines (34 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// UInt8+Bit.swift
//
//
// Created by Joey Z on 12/28/15.
// Github.com/Swift-Kit
// Github.com/josephyzhou
//
import Foundation
extension UInt8 {
// turning UInt8 into an array of Bitss
var bitArray : [Bit] {
// create array of appropriate length:
var array = [Bit](count: 8, repeatedValue: .Zero)
// copy bytes into array
var byteStr = String(self, radix:2)
// pad the string with 0s
let diff = 8 - byteStr.length
if diff > 0 {
for _ in 0..<diff {
byteStr = "0" + byteStr
}
}
// loop through chars, fill the array
for var i = 0; i < byteStr.characters.count; i++ {
if byteStr[i] == "0" {
array[i] = .Zero
} else if byteStr[i] == "1" {
array[i] = .One
}
}
return array
}
}