RainyCats RainyCats - 7 months ago 38
Swift Question

Swift 3 uint64 overflows when trying to assign 64 bit integer

I'm trying to implement bitboards in Swift and am trying to store a 64bit integer into a UInt64 and get an overflow error.

var white_queen_bb:uint64 = 0001000000000000000000000000000000000000000000000000000000000000 as UInt64;

Integer Literal '100000000000000000000000000000000000000000000000000000000000' overflows when stored into 'UInt64'

I'm guessing what happens is that Swift treats the number as a decimal integer and then tries convert it to a binary number which ends up being bigger than 64bits.

Can someone please explain how I would do this.
Thank you


You're right. Swift by default treats numbers as decimal. Therefore let i: UInt64 = 0100 would have the decimal value 100.

If you want to use a binary number in Swift, use the 0b prefix: let j: UInt64 = 0b0100 would have a decimal value of 4.