Why is UInt64 max equal -1 in Swift?

社会主义新天地 提交于 2019-12-01 18:08:07

I think it is a bug of terminal and playground.

But, the output of the println is correct(18446744073709551615) in both terminal and playground.

Looks like the same bug I've run into with any unsigned type.

echo "UInt8(255)" | xcrun swift

will also print -1

It's because your processor is of 64 bit and UInt is 64 bit * 2. The type Int can't run this number, it's out of range.

This is still a bug, even with the REPL in Swift 2.2. See https://bugs.swift.org/browse/SR-1007 or https://bugs.swift.org/browse/SR-1138.

Fixed in Playgrounds with XCode 7.3 though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!