-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I'm sure I'm doing something wrong #11
Comments
Hi @RolandasRazma, I think, this should do the trick: extension Binary {
mutating func readSignedBits(_ quantitiy: UInt8) throws -> Int {
let multiplicationFactor = (try readBit() == 1) ? -1 : 1
let value = try readBits(quantitiy - 1)
return value * multiplicationFactor
}
} It's similar of what you do. First, I tell how many bits I actually want to read. |
hi, no worries, thanks for reply, will try it out |
hm, this gives different result from what I get |
Weird, can you give me an example? |
var binary = Binary(bytes: [252, 100, 99, 4, 254, 129, 98, 19, 254, 3, 200, 192])
_ = try! binary.readBits(quantitiy: 32)
XCTAssertEqual(try binary.readSignedBits(quantitiy: 10), -6) // My implementation returns -6, yours -250
XCTAssertEqual(try binary.readSignedBits(quantitiy: 10), 22)
XCTAssertEqual(try binary.readSignedBits(quantitiy: 10), 132) |
P.S. tested on 3.0.1 |
Hmm weird — on my machine This is what I did for debugging:
Did I miss anything? |
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
The text was updated successfully, but these errors were encountered: