Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the new feature "binary literals" start with 0b instead of being suffixed?

Tags:

c#

roslyn

c#-6.0

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;
like image 594
Vitor Canova Avatar asked Apr 23 '14 12:04

Vitor Canova


People also ask

What is 0B C++?

0b (or 0B ) denotes a binary literal. C++ has allowed it since C++14.

How do you write binary literals?

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.

What is a binary literal?

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.

What is binary literal in Python?

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.


1 Answers

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.

like image 65
Frédéric Hamidi Avatar answered Oct 24 '22 13:10

Frédéric Hamidi