Swift - Convert UInt8 byte to array of bits
Here's a basic function to get a Bit
array from a byte:
func bits(fromByte byte: UInt8) -> [Bit] {
var byte = byte
var bits = [Bit](repeating: .zero, count: 8)
for i in 0..<8 {
let currentBit = byte & 0x01
if currentBit != 0 {
bits[i] = .one
}
byte >>= 1
}
return bits
}
Here, Bit
is a custom enum type that I have defined as follows:
enum Bit: UInt8, CustomStringConvertible {
case zero, one
var description: String {
switch self {
case .one:
return "1"
case .zero:
return "0"
}
}
}
With this setup, the output of the following code:
let byte: UInt8 = 0x1f
print(bits(fromByte: byte))
would be:
[1, 1, 1, 1, 1, 0, 0, 0]
Improving on mohak's answer. With a generic function or an extension to cater for more than just UInt8
.
enum Bit: UInt8, CustomStringConvertible {
case zero, one
var description: String {
switch self {
case .one:
return "1"
case .zero:
return "0"
}
}
}
func bits<T: FixedWidthInteger>(fromBytes bytes: T) -> [Bit] {
// Make variable
var bytes = bytes
// Fill an array of bits with zeros to the fixed width integer length
var bits = [Bit](repeating: .zero, count: T.bitWidth)
// Run through each bit (LSB first)
for i in 0..<T.bitWidth {
let currentBit = bytes & 0x01
if currentBit != 0 {
bits[i] = .one
}
bytes >>= 1
}
return bits
}
extension FixedWidthInteger {
var bits: [Bit] {
// Make variable
var bytes = self
// Fill an array of bits with zeros to the fixed width integer length
var bits = [Bit](repeating: .zero, count: self.bitWidth)
// Run through each bit (LSB first)
for i in 0..<self.bitWidth {
let currentBit = bytes & 0x01
if currentBit != 0 {
bits[i] = .one
}
bytes >>= 1
}
return bits
}
}