How do you cast a UInt64 to an Int64?

前端 未结 6 1423
我在风中等你
我在风中等你 2020-12-17 23:08

Trying to call dispatch_time in Swift is doing my head in, here\'s why:

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_mai         


        
6条回答
  •  一个人的身影
    2020-12-17 23:48

    Casting a UInt64 to an Int64 is not safe since a UInt64 can have a number which is greater than Int64.max, which will result in an overflow.

    Here's a snippet for converting a UInt64 to Int64 and vice-versa:

    // Extension for 64-bit integer signed <-> unsigned conversion
    
    extension Int64 {
        var unsigned: UInt64 {
            let valuePointer = UnsafeMutablePointer.allocate(capacity: 1)
            defer {
                valuePointer.deallocate(capacity: 1)
            }
    
            valuePointer.pointee = self
    
            return valuePointer.withMemoryRebound(to: UInt64.self, capacity: 1) { $0.pointee }
        }
    }
    
    extension UInt64 {
        var signed: Int64 {
            let valuePointer = UnsafeMutablePointer.allocate(capacity: 1)
            defer {
                valuePointer.deallocate(capacity: 1)
            }
    
            valuePointer.pointee = self
    
            return valuePointer.withMemoryRebound(to: Int64.self, capacity: 1) { $0.pointee }
        }
    }
    

    This simply interprets the binary data of UInt64 as an Int64, i.e. numbers greater than Int64.max will be negative because of the sign bit at the most significat bit of the 64-bit integer.

    If you just want positive integers, just get the absolute value.

    EDIT: Depending on behavior, you can either get the absolute value, or:

    if currentValue < 0 {
        return Int64.max + currentValue + 1
    } else {
        return currentValue
    }
    

    The latter option is similar to stripping the sign bit. Ex:

    // Using an 8-bit integer for simplicity
    
    // currentValue
    0b1111_1111 // If this is interpreted as Int8, this is -1.
    
    // Strip sign bit
    0b0111_1111 // As Int8, this is 127. To get this we can add Int8.max
    
    // Int8.max + currentValue + 1
    127 + (-1) + 1 = 127
    

提交回复
热议问题