Casting Int32 to UInt64

Hi people !

With this three liner
Why do I get an arithmetic overflow ?

https://play.crystal-lang.org/#/r/gfq7

data_size is up casted to an UInt64 and the destination is also an UInt64

Hi!

So an int32 can hold: 2_147_483_648 to 2_147_483_647
And an int64 can hold: -9_223_372_036_854_775_808 to 9_223_372_036_854_775_807
And then a Uint64 can hold 0 to 1.8446744e+19
What you are trying to do is 4 ** 256 which is: 1.340781e+154

Since you are dealing with such big numbers so do you have to use the big shard.

Here is a working solution:

require "big"
data_size : Int32 = 4
mask : BigInt = (data_size.to_big_i ** 256) -1
puts mask.to_s(16)
2 Likes