Zig: Proposal: Circular shift operator

Created on 20 Nov 2020  路  3Comments  路  Source: ziglang/zig

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.

proposal

Most helpful comment

math.rotl is only one import away, theres no need to have a thousand builtin functions for every operation.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings