Skip to content
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

[Proposal]: Unsigned right shift operator #4682

Open
1 of 4 tasks
Tracked by #63548
AlekseyTs opened this issue Apr 22, 2021 · 5 comments
Open
1 of 4 tasks
Tracked by #63548

[Proposal]: Unsigned right shift operator #4682

AlekseyTs opened this issue Apr 22, 2021 · 5 comments
Assignees
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion Proposal
Milestone

Comments

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Apr 22, 2021

Unsigned right shift operator

  • Proposed
  • Prototype: Not Started
  • Implementation: Not Started
  • Specification: Not Started

Speclet: https://github.com/dotnet/csharplang/blob/main/proposals/csharp-11.0/unsigned-right-shift-operator.md

Summary

An unsigned right shift operator will be supported by C# as a built-in operator (for primitive integral types) and as a user-defined operator.

Motivation

When working with signed integral value, it is not uncommon that you need to shift bits right without replicating the high order bit on each shift. While this can be achieved for primitive integral types with a regular shift operator, a cast to an unsigned type before the shift operation and a cast back after it is required. Within the context of the generic math interfaces the libraries are planning to expose, this is potentially more problematic as the type might not necessary have an unsigned counterpart defined or known upfront by the generic math code, yet an algorithm might rely on ability to perform an unsigned right shift operation.

Detailed design

There are some considerations around what the operator could be and the initial suggestion is >>>, to match other languages. It is suggested that >>> be explicitly an unsigned right shift and >> would continue being just “right shift” with no strict definition. For primitive unsigned integral types >>> will be allowed, but will be equivalent to >>.

Drawbacks

Alternatives

Unresolved questions

Design meetings

@YairHalberstadt
Copy link
Contributor

Is it common enough to need an operator? Couldn't it just be an ordinary static method?

@KalleOlaviNiemitalo
Copy link

Roslyn recognizes >>>>>>> as the end of a merge conflict but >>> is shorter and so would not conflict with that.

@333fred
Copy link
Member

333fred commented Apr 27, 2021

Roslyn recognizes >>>>>>> as the end of a merge conflict but >>> is shorter and so would not conflict with that.

Also, if we really wanted to use that syntax as an operator, we'd use it. That's just roslyn trying to be helpful, nothing in the C# spec mandates that the compiler produce a good error when encountering merge conflict markers :).

@whoisj
Copy link

whoisj commented Apr 27, 2021

Also nothing mandates that conflict markers be composed only of > characters. 😉

@BillWagner
Copy link
Member

Note: Spec is here

@AlekseyTs AlekseyTs added the Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification label Aug 31, 2022
@jcouv jcouv modified the milestones: Working Set, 11.0 Sep 26, 2022
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion Proposal
Projects
None yet
Development

No branches or pull requests

8 participants