question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bitwise operators ~ | & |= &=

See original GitHub issue

When 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 Sets. 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 the Binary 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 Sets much less than I expected, and Bytes 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 Sets, but also to Binarys, 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:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
luolongcommented, Apr 20, 2018

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.

1reaction
gavinkingcommented, Apr 22, 2018

Done. I’m going to close, but feel free to post feedback here, if any. I can easily reopen the issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found