Question: I'm trying to decode a protobuff encoded message, so I need to convert the first byte (the key) in the protobuff message into bits, so I can find the field number. How do I convert a UInt8 (the byte) into an array of bits?

**Pseudo Code**

```
private func findFieldNum(from byte: UInt8) -> Int {
    //Byte is 0001 1010
    var fieldNumBits = byte[1] ++ byte[2] ++ byte[3] ++ byte[4] //concatentates bits to get 0011
    getFieldNum(from: fieldNumBits) //Converts 0011 to field number, 2^1 + 2^0 = 3
}

```

I saw [this question](https://stackoverflow.com/questions/28929804/swift-bit-array-to-bytes-array-uint8-array), which converts an array of bits into array of bytes.

Answer: 
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]

```
You can do some bitwise operations to get the value of the bits at indices 1-4.

```
// An example byte
let a: UInt8 = 0b00011010

// Extract the bits (using 0b01111000 as a bitmask) 
// in indices 1 to 4, then shift right by 3 places
// to remove indices 5 to 7
var b = (a & 0b01111000) >> 3 

// In this case, b is now 3 (or 11 in binary)
print(b)

```