I have these two numbers, 0xffea1a7f and 0xfffffa95.
Both are signed integer of size 32, evaluates to -1435009 and -1387 respectively.
Crystal parses the numbers as unsigned.
x : Int32 = 0xffea1a7f
y : Int32 = 0xfffffa95
puts "x = #{x}; y = #{y}"
# Error: type must be Int32, not Int64
And Crystal can't do this either:
x : Int32 = 0xffea1a7f_i32
y : Int32 = 0xfffffa95_i32
puts "x = #{x}; y = #{y}"
# Error: 4293532287 doesn't fit in an Int32
This is just an example, others signed integer are having the same behavior. Hex can be signed and unsigned, and we should let Crystal handle these situatin correctly, given that Crystal is supposed to be strong static-typing.
It's not documented in https://crystal-lang.org/reference/syntax_and_semantics/literals/integers.html but those are Int64 integer literals because they exceed the maximum size of Int32.
If you want negative numbers you always have tui use the minus sign. The rules for numbers are not like C.
Most helpful comment
It's not documented in https://crystal-lang.org/reference/syntax_and_semantics/literals/integers.html but those are Int64 integer literals because they exceed the maximum size of Int32.
If you want negative numbers you always have tui use the minus sign. The rules for numbers are not like C.