You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Regular negation of unsigned integers always leads to an underflow for anything other than 0, but wrapped negation is still useful when dealing with binary data or low-level code. Crystal however doesn't support them:
x =1-x # => -1
x =1_u32-x # Error: wrong number of arguments for 'UInt32#-' (given 0, expected 1)&-x # Error: wrong number of arguments for 'UInt32#&-' (given 0, expected 1)
This means one is left with these less clear alternatives:
~x &+1# => 4294967295
x.class.zero &- x # => 4294967295
Thus I propose that the unsigned integer types (UInt8, UInt16, UInt32, UInt64, UInt128) define the argument-less #&- method, which performs wrapped negation. There are some possible extensions:
Signed integer types could define #&- too, but the only affected value is the minimum representable integer of each respective type (in contrast to a hypothetical #- working on exactly one value from each unsigned type), so that's not as useful.
The argument-less #&+ is exactly the same as #+ for integers, so that's probably useless as well.
The text was updated successfully, but these errors were encountered:
Regular negation of unsigned integers always leads to an underflow for anything other than 0, but wrapped negation is still useful when dealing with binary data or low-level code. Crystal however doesn't support them:
This means one is left with these less clear alternatives:
Thus I propose that the unsigned integer types (
UInt8
,UInt16
,UInt32
,UInt64
,UInt128
) define the argument-less#&-
method, which performs wrapped negation. There are some possible extensions:#&-
too, but the only affected value is the minimum representable integer of each respective type (in contrast to a hypothetical#-
working on exactly one value from each unsigned type), so that's not as useful.#&+
is exactly the same as#+
for integers, so that's probably useless as well.The text was updated successfully, but these errors were encountered: