Skip to content

I'm sure I'm doing something wrong #11

@RolandasRazma

Description

@RolandasRazma

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions