bitwise operators ~ | & |= &=
See original GitHub issueWhen I first designed the language, I realized the need to use &
and |
to represent union and intersection of types. Thus, at the value level, it made sense to interpret them as the same operations on Set
s. Ceylon didn’t have bitwise operations at all, because:
- At the time I didn’t view it as a language for pushing bits and bytes around.
- We didn’t even have a
Byte
type, nor theBinary
abstraction, and, furthermore,Integer
did not specify its representation. - I didn’t want to have operator overloading for
&
and|
. - I robbed
^
for exponentiation, and gave it a higher precedence than|
. - Parsing the bitshift operators
>>
and<<
is really much too painful, and I’m always getting confused between>>
and>>>
.
Since then, we introduced a Byte
type, we added well-defined bitwise and bitshifting operations to Integer
, and ultimately I’ve found myself using Set
s much less than I expected, and Byte
s much more. Oh, and we’ve already implicitly overloaded some operators to work on Java types.
Therefore, I propose to overload the operators |
, &
, |=
, and &=
to apply not only to Set
s, but also to Binary
s, and introduce a prefix ~
for bitwise complement.
Note:
- I’m not proposing to add
^
(xor),<<
,>>
, or>>>
. - Overloading these operators introduces an ambiguity, for types that inherit
Set & Binary
, but I think that problem is pretty easy to resolve.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Bitwise operation - Wikipedia
In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral at the level of its...
Read more >Bitwise Operators in C/C++ - GeeksforGeeks
In C, the following 6 operators are bitwise operators (work at ... The ^ (bitwise XOR) in C or C++ takes two numbers...
Read more >Bitwise Operators in C Programming - Programiz
In this tutorial you will learn about all 6 bitwise operators in C programming with examples.
Read more >Bitwise AND (&) - JavaScript - MDN Web Docs
The bitwise AND ( & ) operator returns a 1 in each bit position for which the corresponding bits of both operands are...
Read more >Bitwise Operators in C - Tutorialspoint
Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, i.e., 0011...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
While it is rare to use union and intersection on sets, it is by no means so rare that having operator support for these wouldn’t be useful.
Done. I’m going to close, but feel free to post feedback here, if any. I can easily reopen the issue.