I'm sure I'm doing something wrong, could you point me right direction please? Im trying to read signed value but not sure how to correctly read negative one.
I added category to accomplish this
extension Binary {
mutating func readSignedBits(quantitiy: Int) throws -> Int {
if try readBit() == 1 {
let bit = try readBits(quantitiy: quantitiy - 1)
let bitsReversed = String(String(bit, radix: 2).map({ $0 == "1" ? "0" : "1" }))
return -Int(bitsReversed, radix: 2)! - 1
} else {
return try readBits(quantitiy: quantitiy - 1)
}
}
}
but it doesn't "feel" right
I'm sure I'm doing something wrong, could you point me right direction please? Im trying to read signed value but not sure how to correctly read negative one.
I added category to accomplish this
but it doesn't "feel" right