-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[API Proposal]: Support binary format specifier 'b' and 'B' (standard numeric format strings) #83619
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsBackground and motivation.NET supports a wide range of standard numeric format specifiers when calling API Proposaln/a API UsageThe API would be like so: string byteBinary = ((byte)42).ToString("B");
string intBinary = (42).ToString("b16"); such that: Console.WriteLine(byteString);
Console.WriteLine(intString); will output:
Alternative DesignsEnable all integer types' ( RisksThere should be none as the
|
https://learn.microsoft.com/en-us/dotnet/fsharp/whats-new/fsharp-6#formatting-for-binary-numbers F# added support for |
The design can/should be that all integer types support the |
Would we need to consider the parsing direction as well? e.g. NumberStyles.AllowBinarySpecifier (even though the existing AllowHexSpecifier is, IMHO, poorly named) |
It's probably worth considering them together, yes. I updated the OP to include |
@tannergooding Thanks for the notice. My understanding here is that it would be of value to also implement binary string integer parsing? If so, there are at least three relevant issues currently open related to that. [API Proposal]: Convert.ToString Methods #61719 |
Yes, but those are either of In this case, your proposal actually fits the existing semantics e.g. It then makes sense to consider the inverse (parsing) at the same time and that simply requires two new members to I'll close the other three in favor of this proposal. |
namespace System.Globalization;
[Flags]
public partial enum NumberStyles
{
AllowBinarySpecifier = 0x00000400,
BinaryNumber = AllowLeadingWhite | AllowTrailingWhite | AllowBinarySpecifier,
} |
Everything here is done except for BigInteger. It still needs formatting and parsing |
Hi, I am trying to work on
|
The
The other option which might cause incompatibility with binary parsing would be to substitute the appended (conditional) 0 or 1 with a fixed + or - sign. I'm not sure what the implications would be for fixed-length integer parsing, eg:
Would that be allowed/supported? Back to the original example but using signs, BigInteger could behave like so when printed:
This style of signed binary is sort of like what Google Search does. You can try searching If it's worth anything, my vote goes to the former formatting. |
There is an existing behavior for how |
Hi @RaphaelTetreault thanks, yes I agree we should not prefix signs char in binary.
Just confirm, the output binary should be 2's complement so -106 should output 1001_0110 What I would confirm is binary format definition for non-fixed length's @tannergooding yes I noticed existing hex formatting definition previously, and this sample binary proposal was made by just aligning existing hex formatting as possible and it gives examples for 5 cases. However, because I feel there are still some detailed difference between hex and binary so I would need confirm if it is proper or need to update. Could you help have a look at it ? Just highlight one of differences here: because -921's binary (2's complement)'s highest 1 (non-sign bit) is 10th bit (from 0), which is lower than 13th bit, so current hex format of BigInteger is: C67 rather than FC67 (because one hex char covers 4 bits), so what about binary output for BigInteger for -921 ? (In my sample proposal, it will output 1111_1100_0110_0111 to align one whole byte rather than 1 or 4 bits). |
Notes related to this when working with #28657: The decimal parsing/formatting of BigInteger will easily be shared. The binary/hexadecimal won't be shared because the difference between BigInteger and small integers (arithmetic operations creating new values have huge cost). A rewritten path for Hex/Bin is desired for BigInteger. |
You'd need to return the shortest string. In the case of binary, this would be at most |
So let me try to confirm understanding of desired binary format. based on:
and existing hex format convention (prefixing zero for positive numeric (for some cases), to distinguish from negative numeric with same bits), one of "shortest" string formats for binary is as following:
This way, binary string of positive numeric will always prefix additional zero, for the shortest negative numeric binary format. If not correct, please just provide desired binary content for |
That sounds correct. This then matches that for hex, For binary, then since the smallest unit is
|
Almost same understanding now, except of using 2's complement ? I can see existing hex format output of BigInteger used it, so:
not always be, eg, hex '9' should be -7 If new binary format should also use 2's complement, then binary examples of negative numbers as above should be (note that
Should we also use 2's complement for binary ? |
Yes, sorry. This should have said "hex
Yes. |
Hi, PR is ready for review now, could you please help review, thanks ! |
The We can finish reviewing/merging the remaining PR anytime after main opens for .NET 9 next month. |
Hi @tannergooding just kindly remind PR can be reviewed now as main branch should already open for .NET 9, thanks ! |
Background and motivation
.NET/C# supports a wide range of standard numeric format specifiers when calling
.ToString
on an integer value. Binary numeric formatting is currently only available by callingConvert.ToString([integer], base = 2)
. It would be convenient ifint.ToString([format specifier][precision specifier])
would accept the unused characterb
andB
as the format specifier followed by an optional precision specifier to denote the minimum number of binary digits to display.API Proposal
API Usage
The API would be like so:
such that:
will output:
Alternative Designs
Enable all integer types' (
Int8
,Int16
,Int32
,Int64
,Int128
,UInt8
,UInt16
,UInt32
,UInt64
,UInt128
)ToString
function to accept the format specifierb
andB
with an optional precision specifier in order to output the value's binary representation as astring
.Risks
There should be none as the
b
andB
format specifiers are not currently in use.https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
The text was updated successfully, but these errors were encountered: