Stream: ideas

Topic: Num.bitwiseComplement


view this post on Zulip Kilian Vounckx (Jun 17 2023 at 14:43):

Many languages have a function or operator to calculate the bitwise complement (ones become zeros and vice versa). Roc has bitwiseAnd, bitwiseOr and bitwiseXor, as well as all common bit shift functions. I think a function bitwiseComplement would be a logical addition to complete this set of functions.

I have been making an md5 hash implementation in pure roc for which it is needed. I could get the implementation to work by using Num.maxU32 in combination with Num.bitwiseXor, but this solution is not generic to all integer types.

view this post on Zulip Brendan Hansknecht (Jun 17 2023 at 15:13):

Can we call it bitwiseNot? I think that is a much more common and recognizable name.

view this post on Zulip Brendan Hansknecht (Jun 17 2023 at 15:15):

Also, i think a generic solution is to use Num.subWrap 0 1 for the number type. So should be easy to implement in pure generic roc.

view this post on Zulip Kilian Vounckx (Jun 17 2023 at 15:17):

Brendan Hansknecht said:

Can we call it bitwiseNot? I think that is a much more common and recognizable name.

Yeah sounds better indeed

view this post on Zulip Kilian Vounckx (Jun 17 2023 at 15:17):

Brendan Hansknecht said:

Also, i think a generic solution is to use Num.subWrap 0 1 for the number type. So should be easy to implement in pure generic roc.

Oh cool, I did not think of that, smart!

view this post on Zulip Kilian Vounckx (Jun 17 2023 at 17:59):

Alright, I'll implement it

view this post on Zulip Kilian Vounckx (Jun 17 2023 at 18:59):

PR submitted

view this post on Zulip Hannes (Jun 18 2023 at 11:19):

@Kilian Vounckx I've actually been working on implementing the md5 algorithm in Roc, but it's been slow because I keep getting sidetracked implementing functions like bitwise complement, I didn't think to just ask it to be added to the stdlib :sweat_smile:

view this post on Zulip Kilian Vounckx (Jun 18 2023 at 11:21):

I have a working implementation, haven't benchmarked it though, or tested it with really large values. I will put it on here later when I have my pc with me

view this post on Zulip Kilian Vounckx (Jun 18 2023 at 11:22):

But yeah I had the same experience. I also implemented a chunks function to divide a list into parts. And going from bytes to integers can only be done in one direction iirc

view this post on Zulip Hannes (Jun 18 2023 at 11:28):

Nice, I'm looking forward to see your implementation, then I'll compare with mine and see if I can help with performance

view this post on Zulip Richard Feldman (Jun 18 2023 at 13:21):

Kilian Vounckx said:

But yeah I had the same experience. I also implemented a chunks function to divide a list into parts. And going from bytes to integers can only be done in one direction iirc

as in, there's no way to go from integers to bytes?

view this post on Zulip Kilian Vounckx (Jun 18 2023 at 13:25):

Exactly, There is are the Num.bytesToU__ functions, but no u__ToBytes functions. Also no way to specify endianness, so for md5 I ended up reimplementing the existing ones anyway

view this post on Zulip Richard Feldman (Jun 18 2023 at 14:24):

yeah ok cool, can you start a new #ideas thread for that? I have both thoughts and questions :big_smile:

view this post on Zulip Kilian Vounckx (Jun 18 2023 at 16:02):

Richard Feldman said:

yeah ok cool, can you start a new #ideas thread for that? I have both thoughts and questions :big_smile:

new thread made

view this post on Zulip Kilian Vounckx (Jun 18 2023 at 16:03):

Hannes said:

Nice, I'm looking forward to see your implementation, then I'll compare with mine and see if I can help with performance

Here is what I have. But maybe this should become its own thread :sweat_smile:


Last updated: Jun 16 2026 at 16:19 UTC