-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multicharacter bitwise operators? #545
Comments
Not directly related to answering this question, but one thing to keep in mind is that even if we free up these symbols, we should be cautious as we begin to use them in other contexts that these usages aren't likely to be confusing or surprising for C/C++ programmers to read because of their historical usage as bitwise operators. One easy way of making sure of that is to use them in Carbon as bitwise operators! But this is no motivation for that of course, just something I want us to be mindful of if/when we re-use those symbols. FWIW, many of the concrete examples so far don't seem at all concerning here such as |
It may be helpful to ask what is the alternative here. In particular, how open are we to overloading operators? For example, Another consideration is that there are some bitwise operations for which we don't have operators, like bit rotation, popcount, etc. |
After thinking about this a bunch, I'm largely of the opinion that for familiarity it is reasonably important to keep I also don't think this should preclude using In contrast, I think recovering the I think Based on these thoughts, I'll suggest two possible approaches that folks can vote on in subsequent comments... |
One candidate approach is to try to keep convenient and familiar punctuation syntax for all of these while reclaiming
I quite like this because it follows from complement being the same as xor with all-ones. Since I did think about a few other punctuation candidates here...
One other variation is actually to force writing |
Another candidate approach would be to switch the less common operations to keyword operators. I think my favorite would be:
These have the advantage of exactly matching the alternative operator representations from C++ which seems really nice. They won't collide with C++ variables, etc. And they seem readable and clear. If we really want to consider alternatives, |
FWIW, I mildly prefer the first of these two:
But maybe the last point is my lack of imagination. =] I'd be fine with either though, they both seem solid, reasonably non-inventive, and reasonably friendly to both new programmers and C++ programmers. |
@chandlerc 0 ^ x == x not the complement of x |
Doh, got this backwards. Anyways, 1s ^ x. My point was more that you can use xor for this. LLVM actually does this: https://llvm.org/docs/LangRef.html#xor-instruction |
A third option from Kotlin is to use a member function like (I forgot about this earlier, sorry.) |
I agree with reclaiming |
FWIW, I quite agree. And if we both end up using |
Just to point out, since no-one else has, that C++ allows |
I can accept that argument. I also find |
I believe the practical answer is they're not used: a cursory search shows most uses of Note, this shouldn't be a surprise: I think it should be assumed that both are essentially unused, <1% of source, possibly <0.1%. Note, my own leaning would be towards either symbols or keywords, not a mix of both. I think a mix is more likely to cause confusion. |
We could call |
Calling this converged on:
|
I believe this was included in #1191 and no longer needs to be tracked as "Needs proposal". |
This subject came up when discussing pointer syntax #523 and this doc, but there is plenty of need to free up symbols for other constructs:
|
and/or?
)?
and/or!
)`
) for markdown / documentation@
?)Note that Carbon goals and design decisions (such as reducing ambiguity and types as values) discourage reusing operators for multiple purposes.
We have a few opportunities to free up a few symbols already:
#
and\
?
and:
from the ternary conditional operator (though:
is likely pretty spoken for already in Re-evaluate core variable & parameter identifier/type order (including a default for parameters) #542 )$
and@
aren't in use, and appears to generally be available on keyboards that can type our keywordsnot
instead of!
The bitwise operators were historically also used as boolean operators (search this history for "Neonatal C") which explains why they were given short names and low precedence.
A non-exhaustive list of possible ways of spelling bitwise operators in Carbon:
a \& b
,a \| b
,a \^ b
,\~ b
a &: b
,a |: b
,a ^: b
,~: b
a .&. b
,a .|. b
,a .^. b
,.~. b
a .*. b
,a .+. b
,a .!=. b
,.!. b
a /\ b
,a \/ b
,a (+) b
,-|a
Note: I'm ignoring
<<
and>>
since they are already multiple characters.Question: Should we keep the 1-character symbols for the bitwise operators (
&
,|
,^
,~
) or switch to multiple-character bitwise operators?Note: I am not asking to actually determine the spelling of bitwise operators if we do switch, except in so far as that would help decide the question.
A third choice would be to combine the
^
and~
operators, and use a single symbol for both (prefix usage would be complement and infix would be xor).Advantages of switching to multiple characters:
&
for combining interfaces and address-of,|
for sum types and/or lambdas and/or another kind of bracket,~
for move (C++ uses~
for destructors, which is suggestive of ending the lifetime), and^
might be used with pointers.&
and <0.6% use bitwise^
, but I have low confidence in those numbers. (Let me know if collecting usage statistics would be valuable for making this decision, if I spend some time on it I can get some higher-confidence numbers.)Advantages of preserving:
The text was updated successfully, but these errors were encountered: