How Motoko uses the ‘~’ bitwise negation operator

Nat and Int did not find a corresponding method

I need to use it in my program.

Is there an alternative.

For a language, there is no such basic support, which surprised me

How Motoko uses the ‘~’ bitwise negation operator

Nat and Int did not find a corresponding method

I need to use it in my program.

Is there an alternative.

For a language, there is no such basic support, which surprised me

Hey @avi. You can only use `~`

on `Nat64`

(and lower), not on `Nat`

directly.

So you’ll have to convert them first (i.e. `Nat64.fromNat(n)`

)

Nat64 When I am using a very large value, an error occurs

Yes, you can only use this for values under `18446744073709551615`

.

You’ll have to write your own logic, afaik there is no package that supports this atm.

1 Like

Although I know that this can only be done, shouldn’t this be something that should be considered at the very beginning of a design language?

Nat and Int are arbitrary precision and have morallly infinite bitwidth. Does it actually make sense to support bitwise negation for them? I’m pretty sure it does not, but am happy to be educated otherwise.

Yes, two-complement works for infinite bitwidths as well; negative numbers have all the infinite number of high bits set. See for example the `Bits`

instance of `Integer`

in Haskell.

Opened an issue Implement `~` for Nat and Int · Issue #2799 · dfinity/motoko · GitHub for triage/tracking.

1 Like