 
     func intFromBits(count: Int) -> Int {
         guard count > 0 else { return 0 }
+
         var result = 0
         for i in 0..<count {
-            let currentIndex: Int
             let power: Int
             switch self.bitOrder {
             case .straight:
-                currentIndex = 8 * index + bitShift
                 power = count - i - 1
             case .reversed:
-                currentIndex = 8 * (index + 1) - bitShift - 1
                 power = i
             }
 
-            result += Int(pow(Double(2), Double(power))) *
-                Int(bitPattern: UInt(CFBitVectorGetBitAtIndex(self.bitVector, currentIndex)))
+            let bit = self.bitArray[self.index] & self.bitMask > 0 ? 1 : 0
+            result += Int(pow(Double(2), Double(power))) * bit
 
-            self.bitShift += 1
-            if self.bitShift >= 8 {
-                self.bitShift = 0
-                self.index += 1
+            switch self.bitOrder {
+            case .reversed:
+                if self.bitMask == 128 {
+                    self.index += 1
+                    self.bitMask = 1
+                } else {
+                    self.bitMask <<= 1
+                }
+            case .straight:
+                if self.bitMask == 1 {
+                    self.index += 1
+                    self.bitMask = 128
+                } else {
+                    self.bitMask >>= 1
+                }
             }
         }
 

        return self.bits(count: 1).first!
    }

    /// Skips until next byte and returns next `count` bytes.
    func alignedBytes(count: Int) -> [UInt8] {
        self.skipUntilNextByte()
        var array: [UInt8] = Array(repeating: 0, count: count)
        CFBitVectorGetBits(self.bitVector, CFRangeMake(self.index * 8, count * 8), &array)
        self.index += count
        return array
    }

    func alignedByte() -> UInt8 {
        self.skipUntilNextByte()
        var array: [UInt8] = [0]
        CFBitVectorGetBits(self.bitVector, CFRangeMake(self.index * 8, 8), &array)
        self.index += 1
        return array.first!
    }

    // MARK: Manipulations with index and bitShift

    func skipUntilNextByte() {
        guard self.bitShift != 0 else { return }
        self.index += 1
        self.bitShift = 0
    }

}


        var array: [UInt8] = Array(repeating: 0, count: count)
        for i in 0..<count {
            array[i] = self.bitArray[self.index] & self.bitMask > 0 ? 1 : 0

            switch self.bitOrder {
            case .reversed:
                if self.bitMask == 128 {
                    self.index += 1
                    self.bitMask = 1
                } else {
                    self.bitMask <<= 1
                }
            case .straight:
                if self.bitMask == 1 {
                    self.index += 1
                    self.bitMask = 128
                } else {
                    self.bitMask >>= 1
                }
            }
        }

