Question

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;
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top