in Sources/TSCUtility/Bits.swift [25:48]
func readBits(atOffset offset: Int, count: Int) -> UInt64 {
precondition(count >= 0 && count <= 64)
precondition(offset >= 0)
precondition(offset &+ count >= offset)
precondition(offset &+ count <= self.endIndex)
return buffer.contents.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
let upperBound = offset &+ count
let topByteIndex = upperBound >> 3
var result: UInt64 = 0
if upperBound & 7 != 0 {
let mask: UInt8 = (1 << UInt8(upperBound & 7)) &- 1
result = UInt64(bytes[topByteIndex] & mask)
}
for i in ((offset >> 3)..<(upperBound >> 3)).reversed() {
result <<= 8
result |= UInt64(bytes[i])
}
if offset & 7 != 0 {
result >>= UInt64(offset & 7)
}
return result
}
}