fsharp / fsharp/fslang-suggestions
Unsigned right shift operator
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
# Unsigned right shift operator
I propose we add the unsigned right shift operator to F#. Since [C# is looking to add this operator](https://github.com/dotnet/csharplang/issues/4682), and F# already has all the bit-twiddling operators equivalent to C#, it only makes sense for F# to also add this operator for whatever C# has reasons for:
> 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.
The existing way of approaching this problem in F# is
```fs
// Quote: a cast to an unsigned type before the shift operation and a cast back after it is required.
let (>>>>) (value:int) pos = value |> uint >>> pos |> int
```
However, as mentioned in the quote above, this is not applicable to generic math code. Although for F# we have SRTP, it is still desirable to have this defined and built-in for standardisation.
## Pros and Cons
The advantages of making this adjustment to F# are
1. Making generic math algorithms easier
1. Consistency with C#
1. Consistency with OCaml: We have `<<<` for `lsl`, `>>>` for `asr` and now we finally have an equivalent for `lsr`.
The disadvantage of making this adjustment to F# is that this may be too niche to warrant its own operator.
## Extra information
Estimated cost (XS, S, M, L, XL, XXL): XS - Just copy the implementation for `>>>` and use `shr.un` instead of `shr`.
Related suggestions: (put links to related suggestions here)
## Affidavit (please submit!)
Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.