csharplang/proposals/csharp-7.0/binary-literals.md

33 lines
1 KiB
Markdown
Raw Normal View History

Binary Literals
===============
Theres a relatively common request to add binary literals to C# and VB. For bitmasks (e.g. flag enums) this seems genuinely useful, but it would also be great just for educational purposes.
Binary literals would look like this:
``` c#
int nineteen = 0b10011;
```
Syntactically and semantically they are identical to hexadecimal literals, except for using `b`/`B` instead of `x`/`X`, having only digits `0` and `1` and being interpreted in base 2 instead of 16.
Theres little cost to implementing these, and little conceptual overhead to users of the language.
# Syntax
The grammar would be as follows:
> _integer-literal:_
>   ...
>   _binary-integer-literal_
>
> _binary-integer-literal:_
>   `0b`   _binary-digits_   _integer-type-suffixopt_
>   `0B`   _binary-digits_   _integer-type-suffixopt_
>
> _binary-digits:_
>   _binary-digit_
>   _binary-digits_   _binary-digit_
>
> _binary-digit:_   one of
>   `0` `1`