The next C# version is planned (April 2014) to have binary literals as you can see in the Language Features Status of Roslyn project.
The example in that page is like this:
0b00000100
So you probably will use like this:
var myBynaryLiteral = 0b00000100;
I want to understand why they choose to prefix this with 0b
instead of use a letter in the end like they did with double
, float
, decimal
and so on.
double a = 1d;
float b = 1f;
decimal c = 1m;
0b (or 0B ) denotes a binary literal. C++ has allowed it since C++14.
Binary literals can be written in one of the following formats: b'value' , B'value' or 0bvalue , where value is a string composed by 0 and 1 digits. Binary literals are interpreted as binary strings, and are convenient to represent VARBINARY, BINARY or BIT values.
A binary literal is a number that is represented in 0s and 1s (binary digits). Java allows you to express integral types (byte, short, int, and long) in a binary number system. To specify a binary literal, add the prefix 0b or 0B to the integral value.
Binary literals begin with a leading 0b or 0B, followed by binary digits (0-1). All of these literals produce integer objects in program code; they are just alternative syntax for specifying values. The built-in calls hex(I), oct(I), and bin(I) convert an integer to its representation string.
Integer literals possess two varying properties: their types, which can be specified with suffixes like L
or UL
, and their radices (called "forms" in the documentation), which can be specified with prefixes like 0x
and now 0b
.
Specifying a type was always done through a suffix, and specifying a radix was always done through a prefix, so it makes sense to keep the same convention. In addition, you can combine both specifiers.
For instance:
0b00101010UL
Would denote the literal 42
, stored as an unsigned long, and expressed in radix 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With