Open mrakh opened 3 years ago
I think the standard meaning of %
elsewhere in the language (discard overflow) is kind of the opposite of this use. Other than that though I think this is worth doing. Maybe as a builtin instead?
I think the standard meaning of
%
elsewhere in the language (discard overflow) is kind of the opposite of this use. Other than that though I think this is worth doing. Maybe as a builtin instead?
For addition, subtraction and multiplication with %
, there is no distinction between wraparound vs. overflow/underflow, since the operations are performed modulo a power of two. The %
token is described as having wrapping behavior in the docs, so that's what I went with. But if the operator is too obscure, I suppose a built-in would serve just as well.
math.rotl
is only one import away, theres no need to have a thousand builtin functions for every operation.
@mrakh I'm wondering about this myself, for a different language though.
These have come up as options:
rotate right: @>
rotate left: <@
One can devise variants of these too.
rotate right: >@>
rorate left: <@<
Not sure about Zig, in my case @ is able to be used in a token like this.
An option I like is <<<
and >>>
from Jai. I see the point about just using a library function for this, though.
No opinion about this, just wanted to note that in Java >>
sign-extends while >>>
shifts in 0s.
I generally agree we don't need operators (nor builtins) for everything, though I don't know how to justify what to include and what not to.
i think we're safe to have >>>>
<<<<
for being infrequently used, cheap, and cool.
also see #5220 like proposals.
No opinion about this, just wanted to note that in Java
>>
sign-extends while>>>
shifts in 0s. I generally agree we don't need operators (nor builtins) for everything, though I don't know how to justify what to include and what not to.
In my opinion, what we include as fundamental operators affects how people think about and approach problems. If not given an operator, it has more friction to use it, and fewer people will know about it, even though it's a 1 cycle operation on pretty much all hardware, a fact that I think most programmers just forget about since it is not an operator.
I personally think an operator, not a builtin, is the appropriate level of at-your-fingertips Zig should provide for such a simple, low-level, and widely supported operation.
<<<
and >>>
are my preferences. Yes, a Java or JavaScript person might need to learn that these are rotate operators, but that's fine, they can learn that.
Im more in favor of the >>%
, <<%
version personally, given the wrapping idea common to %
-based operators for arithmetic
Circular shift (or bit rotation) is a common operation to perform and most every architecture out there has hardware support for it. Since we already use the
%
token to qualify existing operators with wraparound behavior, I propose we use>>%
/>>%=
/<<%
/<<%=
to denote circular shift operations.