Skip to content

Allow binary numbers, byte literals, and BigInteger? #7557

Closed

Description

I will attempt to summarise here the primary points of discussion that have ensued in #7993 as it has spiraled into many threads, and I suspect a bullet-point summary of questions to be answered will be significantly easier on the committee.

Regarding Binary & Hex Parsing

As part of the refactor & introduction of binary parsing, methodology of hex parsing has also been altered a bit. Parsing currently results in the same as it currently works, with the caveat that literals with values above Int64.MaxValue are also now acceptable.

Parse as C# Literals

With that in mind, mklement brought up the point that we may want to simply change how hex and binary parsing work. That is, mimic C#'s behaviour of these literals in source, which would mean parsing all hexadecimal literals as strictly positive (no more 0xFFFFFFFF -eq -1 — instead, 0xFFFFFFFF -eq UInt32.MaxValue) and having such literals smoothly convert up to UInt values.

With that in mind, the code patterns for hex or binary literals would seek out the lowest available parsed value type (when no type is specified) in the following order: Int32, UInt32, Int64, UInt64, Decimal, and possibly finally BigInteger.

Other Options

Hex & BigInteger

If we elect to keep current hex behaviour, we need to consider how it would behave in ranges higher than Decimal. BigInteger's default parser for hex numerals will simply assume the highest bit of a byte is indicative of the sign. As a result any numeral treated as signed that begins with 0x8 or higher will be considered the negative two's complement representation when we enter ranges that can only be parsed as BigInteger. This could be overridden easily, if this behaviour is considered to be undesirable.

Binary Parsing with Sign Bits

Then we face the issue of what to do about binary parsing. I doubt most folks working with binary directly will be working in ranges above 32-bit numbers, but I could be very wrong on that. They are, however, easier to work with in the byte, short, and int value ranges (8, 16, 32-length literals), and behaviour of a sign bit in this case is also entirely up to the parser here due to the custom implementation of binary parsing for speed concerns.

Should binary sign bits only be accepted at 32-bit lengths and up for consistency with hex parsing? Or should they be accepted at similar lengths of literal (8 binary bits, 8 hex char literal) to match up visually with hex literals? This would place a sign bit at all of the 8, 16, and 32-char lengths of a binary literal, so 0b11111111 -eq -1 and so forth, which looks similar in behaviour to hex's 0xFFFFFFFF -eq -1, despite the obvious difference in actual bit length of the literals.

Parse Numeric Literals with Underscores

E.g., 1_000_000, 0b0101_1010, 0xF_FFF_F_FFF and so forth. Should this be allowed? C# already does this with literals in source code. Are there culture-sensitive concerns around this? This is a relatively simple addition.

Experimental Feature Possibilities

If this is the best option, I am not at all against hiding alternate parse methods behind experimental flags if need be. But for that to be possible, I need a "standard" acceptable behaviour to be defined clearly so that I can lay it out for the hex and binary parse methods.


Original post is below. PR #7901 added byte literals (suffix y or uy), so that portion of this issue is completed.


See the discussion in #7509.

Emerging from the interesting dust of modifying the tokenizer are two further points:

  1. The tokenizer should be able to parse binary literals.
  2. The tokenizer should support byte type literals.

The trouble here is that both of these suggestions could arguably use a b suffix for numeric literals.

My opinion is that the b suffix should be used for byte literals, in keeping with the current convention that suffixes alter the numeric data type rather than simply base representation.

So what about binary? Well, jcotton in the PowerShell Slack channel / irc / discord #bridge channel mentioned that just like common hex representations use 0xAAFF057, we could also follow the common convention of binary being similar: 0b01001011

From my brief poking about, it looks like we may have to alter System.Globalization.NumberStyles in order to add Binary as a flag value -- if we follow the current implementation of hexadecimal numerals. We don't necessarily have to.

TryGetNumberValue in the tokenizer.cs file would also have to be modified to accept possibly some kind of enum for number formats as well; currently it only accepts a bool for hex specification. ScanNumberHelper would also have to be modified for this.

The suffix approach is simpler, especially with the changes already in #7509 which make adding suffixes a good deal easier. However, given that we may want to reserve the b suffix for 123b byte literals, we may need to consider adding a case for 0b0010101 syntax.

What do you guys think?

Other suggested suffixes for byte literals:

  • ub ( sb or b for signed bytes)
  • uy ( F# style) with y for signed bytes

Activity

  1. rjmholt commented on Aug 17, 2018

    @rjmholt
    Collaborator

    +1 on:

    • b suffix is for bytes
    • 0b prefix is for binary

    My only problem is that the most convenient way to specify a byte will be in hex, and 0xffb can't specify a byte because its a valid ordinary hex literal.

    It might not be possible to accomplish nicely, but my ideal would be able to specify a byte literal with hex.

  2. vexx32 commented on Aug 17, 2018

    @vexx32
    CollaboratorAuthor

    We could adopt the uy suffix like F# for bytes, in that case?

  3. iSazonov commented on Aug 17, 2018

    @iSazonov
    Collaborator

    Could we use 'ub' for bytes and 'b' for binaries.
    The use of the '0b' prefix does not seem to be consistent.

  4. added
    Issue-Enhancement
    WG-Language
    Review - Committee
    on Aug 17, 2018
  5. vexx32 commented on Aug 17, 2018

    @vexx32
    CollaboratorAuthor

    we could use anything we want, in fact. Just gotta code for it in the tokenizer... and I've had plenty of experience doing that recently :)

  6. jcotton42 commented on Aug 17, 2018

    @jcotton42
    Contributor

    Ilya (@iSazonov) every language I've run across uses an 0b prefix for binary numbers. Plus using a prefix to change the base is consistent with 0x for hex.

  7. vexx32 commented on Aug 17, 2018

    @vexx32
    CollaboratorAuthor

    If we take the F# route they also have y for sbyte which... I don't really know how useful it is, but it's a possibility.

    With this sort of syntax your literals would look like:

    255uy
    0b1001uy # decimal 9; byte
    0xAAuy # decimal 160; byte
    
    12y # sbyte
    0b11y  # decimal 3; sbyte
    0x50y # decimal 80; sbyte
  8. iSazonov commented on Aug 17, 2018

    @iSazonov
    Collaborator

    We usually consider C# syntax first. In C# 7 we get '0b' for binary literals but there is still not exist a suffix for byte. I think we should try to find a discussion in .Net repos about the suffix - I guess they already have it. If the discussion is still not finished we have to postpone the decision on the suffix to keep a consistency with C# in future.
    For binary we could support a format
    var b = 0b1010_1011_1100_1101_1110_1111;

  9. vexx32 commented on Aug 17, 2018

    @vexx32
    CollaboratorAuthor

    The link to Roslyn here seems to be defunct as they shifted to Github, but nonetheless the quote seems to indicate:

    https://stackoverflow.com/questions/5378036/literal-suffix-for-byte-in-net

    They went for zero ambiguity and decided sb for signed and ub for unsigned (since .NET bytes are default unsigned, but everything else is usually signed as standard).

  10. iSazonov commented on Aug 17, 2018

    @iSazonov
    Collaborator

    I think it is here dotnet/csharplang#1058 without any progress.

  11. vexx32 commented on Aug 17, 2018

    @vexx32
    CollaboratorAuthor

    For me, the suffix b in general might be a bit lacklustre for byte simply because to a new user it could easily be read as bit, or binary instead and be confused. At least with y if they don't know they're probably more likely to actually look it up rather than guessing and being burned by it.

    But I guess if we want to stick with parity we might want to stick with the most likely candidates for inclusion in C#.

    It's also a non-zero possibility that implementing this in PS may affect the discussion of inclusion in C# as it remains open after almost a year.

  12. iSazonov commented on Aug 17, 2018

    @iSazonov
    Collaborator

    I commented in csharplang repo.

  13. rjmholt commented on Aug 17, 2018

    @rjmholt
    Collaborator

    Also want to note that underscore syntax you used there Ilya (@iSazonov). I was thinking about the same thing the other night. I think it would be worth implementing underscore ignoring in numeric literals too (separate issue I know, but thought I'd raise it here first in case other people think it's stupid). It looks like C# 7, Java 7 and OCaml have this already, and it would certainly make sense alongside a binary literal syntax.

  14. 42 remaining items

  15. vexx32 commented on Oct 7, 2018

    @vexx32
    CollaboratorAuthor

    Thanks Steve!

    HumanEquivalentUnit and I have discovered a new BigInteger constructor introduced in .NET Core 2.0 and have been toying with parsing binary using it:

    public BigInteger(ReadOnlySpan<byte> value, bool isUnsigned = false, bool isBigEndian = false)

    Benchmarks of a bunch of different methods are looking very good indeed. Some of the slower possible methods we've come up with (he's been doing a lot of the tinkering here) are still about twice as fast as Convert.ToIntX() methods, even with smaller numbers. 😄

    Beyond that, we're subdividing nanoseconds finding quicker methods, so I think I can call that "good enough" for the forseeable future!

  16. iSazonov commented on Oct 12, 2018

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) We need PowerShell Committee conclusion to continue, please.

  17. mklement0 commented on Oct 22, 2018

    @mklement0
    Contributor

    Re use of only uppercase I for [bigint], quoting #7993 (comment)

    Adds support for natively returning a biginteger with no rounding using the I (capital i) suffix. I elected to use "big i" and exclude "little i" as that is generally reserved in mathematics for imaginary numerals and could be confusing to some.

    While lowercase i can indeed get confusing, I suggest not introducing an inconsistency by making the I suffix the lone exception in terms of case-sensitivity.

    While it makes sense to document and recommend the use of uppercase I, and to use it in examples, I suggest accepting i too, for symmetry with all other type-suffix characters.

    Alternatively, we could pick a different character - has n been considered? (It is pretty much the only other letter left that doesn't cause outright confusion; any letter we choose is technically a breaking change in argument mode).

  18. vexx32 commented on Oct 22, 2018

    @vexx32
    CollaboratorAuthor

    n is a pretty decent alternative... I'd rather not cause confusion for those more mathematically inclined, I think. Worth considering also, definitely.

    (And since it's trivially easy to implement complex numeral parsing, I'd rather not completely block that out as a possibility by taking i as a suffix here, although in our target userbase I doubt there's a lot of use for it.)

  19. changed the title [-]Allow binary numbers and/or byte type literals[/-] [+]Allow binary numbers, byte literals, and BigInteger?[/+] on Nov 15, 2018
  20. vexx32 commented on Nov 15, 2018

    @vexx32
    CollaboratorAuthor

    Steve Lee (@SteveL-MSFT) Michael Klement (@mklement0) Ilya (@iSazonov)

    I have attempted to summarize in the original issue description the main discussion points from #7993 as best I can to assist with committee review of the primary sticking points on that PR. 😄

  21. SteveL-MSFT commented on Nov 15, 2018

    @SteveL-MSFT
    Member

    Rain Sallow (/u/ta11ow) (@vexx32) Thanks for the summary, this will help the review and come to a conclusion more quickly!

  22. SteveL-MSFT commented on Nov 28, 2018

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this, regarding the underscore, we do not accept adding that as it can cause issues with existing usage as Jason Shirk (@lzybkr) pointed out. Also, we do not want auto coercion from long to bigint as not only is this a breaking change, but may cause surprising effects to users.

  23. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Nov 28, 2018
  24. added a commit that references this issue on Apr 3, 2019
    3f52adb
  25. added a commit that references this issue on Aug 6, 2026
    273705d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedIssue-EnhancementResolution-FixedWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions