Title: Allow binary numbers, byte literals, and BigInteger? · Issue #7557 · PowerShell/PowerShell · GitHub
Open Graph Title: Allow binary numbers, byte literals, and BigInteger? · Issue #7557 · PowerShell/PowerShell
X Title: Allow binary numbers, byte literals, and BigInteger? · Issue #7557 · PowerShell/PowerShell
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 commit...
Open Graph 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 wi...
X 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 wi...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/7557
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow binary numbers, byte literals, and BigInteger?","articleBody":"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. \r\n\r\n## Regarding Binary & Hex Parsing\r\n\r\nAs 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.\r\n\r\n### Parse as C# Literals\r\n\r\nWith 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. \r\n\r\nWith 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`.\r\n\r\n### Other Options\r\n\r\n#### Hex & BigInteger\r\n\r\nIf 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.\r\n\r\n#### Binary Parsing with Sign Bits\r\n\r\nThen 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.\r\n\r\nShould 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.\r\n\r\n## Parse Numeric Literals with Underscores\r\n\r\nE.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.\r\n\r\n## Experimental Feature Possibilities\r\n\r\nIf 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.\r\n\r\n---\r\n\r\nOriginal post is below. PR #7901 added byte literals (suffix `y` or `uy`), so that portion of this issue is completed.\r\n\r\n---\r\nSee the discussion in #7509.\r\n\r\nEmerging from the interesting dust of modifying the tokenizer are two further points:\r\n\r\n1. The tokenizer should be able to parse binary literals.\r\n2. The tokenizer should support `byte` type literals.\r\n\r\nThe *trouble* here is that both of these suggestions could arguably use a `b` suffix for numeric literals.\r\n\r\nMy 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.\r\n\r\nSo 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`\r\n\r\nFrom 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. \r\n\r\n`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.\r\n\r\nThe 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.\r\n\r\nWhat do you guys think?\r\n\r\nOther suggested suffixes for byte literals:\r\n\r\n* `ub` ( `sb` or `b` for signed bytes)\r\n* `uy` ( F# style) with `y` for signed bytes\r\n","author":{"url":"https://github.com/vexx32","@type":"Person","name":"vexx32"},"datePublished":"2018-08-17T01:13:35Z","url":"https://github.com/PowerShell/PowerShell/issues/7557"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f6a50778-22af-8439-39e6-6edbe8f7b517 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BB26:19E595:1975B9:2210FE:6AFD4300 |
| html-safe-nonce | d674c2332bd4de5b11ac4532f0cd74a3897fae56a3f90e61d94ef97f8e2edbbf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQjI2OjE5RTU5NToxOTc1Qjk6MjIxMEZFOjZBRkQ0MzAwIiwidmlzaXRvcl9pZCI6IjUwNzcwOTk3MTg3Nzc4NDg1NzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 46185f71b6a2849972543246fe1c522f9b7af5819987cd5f6e4086a7e22241c7 |
| hovercard-subject-tag | issue:351423441 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/7557/issue_layout |
| twitter:image | https://opengraph.githubassets.com/545bb6747a7a1182d997722c34e6833fe50fd85bab90cbf037babd86c60bf5ef/PowerShell/PowerShell/issues/7557 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/545bb6747a7a1182d997722c34e6833fe50fd85bab90cbf037babd86c60bf5ef/PowerShell/PowerShell/issues/7557 |
| og:image:alt | 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 wi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vexx32 |
| hostname | github.com |
| expected-hostname | github.com |
| None | bdbacc25b921224ff0befcb9e629560a1255be51aec9991d6e3f95b5e9151474 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 556dd37bb0aceffb25724e151b5d858b3265b40b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width