I need a way to deal with Negative UINT64

Yes I know… the whole point of UINT is that it is UNSIGNED…

But I need a way so if a NEGATIVE value is passed into a function… it still “works”

-1 as a INT64 is “FFFFFFFFFFFFFFFF” while that same hex pattern for UINT64 is 18,446,744,073,709,551,615

So for all values from -Int64.min to Uint64.max … return the Uint64 that matches the same bit pattern

If I had a Int128 datatype I could do it…

NOTE : this is Swift not Xojo (so that BigNumbers module isn’t an option)

Haven’t tested it yet… .but I think I found a routine the basicly stuffs a Int64 into a Swift memoryblock and then extracts it as a Uint64…

extension Int64 {
	var unsigned: UInt64 {
		let valuePointer = UnsafeMutablePointer<Int64>.allocate(capacity: 1)
		defer { valuePointer.deallocate(capacity: 1) }
		valuePointer.pointee = self
		return valuePointer.withMemoryRebound(to: UInt64.self, capacity: 1) { $0.pointee }
let x : Int64 = -1
let y : UInt64 = x.unsigned


BINGO! :slight_smile:

that is JUST a cast in many langs

dim ui64 as Uint64 = Uint64(&hFFFFFFFFFFFFFFFF)
dim i64 int64 = Int64(ui64)

same in c/c++
sizes are the same etc

let x : Int64 = -1
let y : UInt64 = UInt64(x)

Fatal error: Negative value is not representable

how odd

what about

let x: Int64 = 0x1234abcd
let y: UInt64 = UInt64(bitPattern: x)


the goal is just to reinterpret the existing bits

Cool!.. that works as well it seems… I would never have found that…