Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement bitops for Nat and Int #2799

Open
Tracked by #3921
crusso opened this issue Sep 21, 2021 · 5 comments
Open
Tracked by #3921

Implement bitops for Nat and Int #2799

crusso opened this issue Sep 21, 2021 · 5 comments
Assignees
Labels
compiler Motoko → Wasm language design Requires design work P2 medium priority, resolve within a couple of milestones

Comments

@crusso
Copy link
Contributor

crusso commented Sep 21, 2021

A user has requested an infinite precision bitwise negation operator on Nat and Int.

https://forum.dfinity.org/t/how-motoko-uses-the-bitwise-negation-operator/7428/8

Should be easy to support, acc to @nomeata, pointing at:

https://github.com/libtom/libtommath/blob/develop/mp_complement.c

@crusso crusso added compiler Motoko → Wasm language design Requires design work P2 medium priority, resolve within a couple of milestones labels Sep 21, 2021
@crusso
Copy link
Contributor Author

crusso commented Sep 21, 2021

(don't forget to check the doc!)

@crusso
Copy link
Contributor Author

crusso commented Sep 21, 2021

@rossberg any reason not to?

@nomeata
Copy link
Collaborator

nomeata commented Sep 21, 2021

I think only Int.

Surely not ~ without all the other bitwise operations (category B in https://sdk.dfinity.org/docs/language-guide/language-manual.html#syntax-ops), right? (Although right rotation doesn’t make sense for Int)

@rossberg rossberg changed the title Implement ~ for Nat and Int Implement bitops for Nat and Int Sep 23, 2021
@rossberg
Copy link
Contributor

rossberg commented Sep 23, 2021

Agreed with @nomeata: only makes sense to do this if we do all the bitops – I changed the issue title accordingly. Also, FWIW, there is no ~ op in Motoko, it's called unary ^. ;)

So I'd expect:

^ : Int -> Int
&, |, >>, << : (Nat, Nat) -> Nat /\ (Int, Int) -> Int
^ : (Int, Int) -> Int

^= : (var Int, Int) -> ()
&=, |=, >>=, <<= : (var Nat, Nat) -> () /\ (var Int, Int) -> ()
^= : (var Int, Int) -> ()

The only ones that make sense for neither Nat nor Int are <<> and <>> (strictly speaking, you could define <<>, but it seems pretty pointless).

So, yes, all doable, but a non-trivial amount of work, and given everything else, not high priority.

@crusso crusso mentioned this issue Apr 4, 2023
79 tasks
@ggreif
Copy link
Contributor

ggreif commented Jul 16, 2024

At least we have

shiftLeft : (Nat, Nat32) -> Nat;
shiftRight : (Nat, Nat32) -> Nat;

in the primitives now.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
compiler Motoko → Wasm language design Requires design work P2 medium priority, resolve within a couple of milestones
Projects
None yet
Development

No branches or pull requests

4 participants