Proposal: Allow binary literals

46 views
Skip to first unread message

Peter Kasting

unread,
Jan 8, 2018, 10:54:40 PM1/8/18
to cxx
C++14 allows defining binary literals directly, using the "0b" prefix.

I propose to allow these.  There are some theoretical concerns with editors not detecting these as integral constants for syntax highlighting, but I suspect actual problems will be minor (for example, Visual Studio's editor handles these just fine).

It's not common that binary literals are clearer than, say, hex, but masks whose fields don't align on byte boundaries are one case; see https://chromium-review.googlesource.com/c/chromium/src/+/855823/1/ui/gfx/x/x11_types.cc .

PK

Brett Wilson

unread,
Jan 8, 2018, 11:06:33 PM1/8/18
to Peter Kasting, cxx
+1

Brett

Avi Drissman

unread,
Jan 8, 2018, 11:13:18 PM1/8/18
to Brett Wilson, Peter Kasting, cxx
+1 but I would request that we accept number literal separators at the same time. Readability of binary values is highly improved with those.

Avi

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CABiGVV8_1G5ihioZ-mNQFWWE%3DQ_Y8Sxjd_sDfkSSxNppEovusw%40mail.gmail.com.

Peter Kasting

unread,
Jan 8, 2018, 11:14:24 PM1/8/18
to Avi Drissman, Brett Wilson, cxx
On Mon, Jan 8, 2018 at 8:12 PM, Avi Drissman <a...@chromium.org> wrote:
+1 but I would request that we accept number literal separators at the same time. Readability of binary values is highly improved with those.

I thought about arguing for digit separators, but worried that they could increase the risk of editor incompatibility.  It's also not totally clear where one puts the separator in a binary literal (every byte? every nybble?).

All this leaves me kind of ambivalent on them, so I abstain from that argument :)

PK

Avi Drissman

unread,
Jan 8, 2018, 11:40:47 PM1/8/18
to Peter Kasting, Brett Wilson, cxx
I already see editor incompatibility with things like raw literals that we allow today, so I don't personally take that very much into consideration. (Dunno what the official position is though.)

On the other hand, in your sample CL I see separators as both incredibly useful and if needed, obvious as to position:

    image.red_mask = 0b1111100000000000;
    image.green_mask = 0b0000011111100000;
    image.blue_mask = 0b0000000000011111;

vs

    image.red_mask = 0b11111'000000'00000;
    image.green_mask = 0b00000'111111'00000;
    image.blue_mask = 0b00000'000000'11111;

Why restrict yourself to the byte/nybble? For something like this, actually splitting the 16 bits as 5'6'5 makes it super clear what's going on. I would say "use where appropriate for clarity" and leave it to whomever actually is writing binary constants to use them as needed/if needed.

Avi

Chris Blume

unread,
Jan 9, 2018, 12:05:00 AM1/9/18
to Avi Drissman, Peter Kasting, Brett Wilson, cxx
I absolutely LOVE both binary literals and digit separators.
And I agree with the whatever-makes-sense. I suspect if something weird came up in a code review like 0b11'0'1'10'1 the reviewer would call it out.


Chris Blume |
 Software Engineer | cbl...@google.com | +1-614-929-9221


--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.

Peter Kasting

unread,
Jan 9, 2018, 2:13:54 AM1/9/18
to Avi Drissman, Brett Wilson, cxx
On Mon, Jan 8, 2018 at 8:40 PM, Avi Drissman <a...@chromium.org> wrote:
On the other hand, in your sample CL I see separators as both incredibly useful and if needed, obvious as to position:

    image.red_mask = 0b1111100000000000;
    image.green_mask = 0b0000011111100000;
    image.blue_mask = 0b0000000000011111;

vs

    image.red_mask = 0b11111'000000'00000;
    image.green_mask = 0b00000'111111'00000;
    image.blue_mask = 0b00000'000000'11111;

Why restrict yourself to the byte/nybble? For something like this, actually splitting the 16 bits as 5'6'5 makes it super clear what's going on. I would say "use where appropriate for clarity" and leave it to whomever actually is writing binary constants to use them as needed/if needed.

That looks super-weird to me!  I read the separators as indicating bytes, so this reads like 0x1f0000 / 0x003f00 / 0x00001f.  As a reviewer I'd ask you to change it since it feels so misleading :)

It looked fine to me without separators when lined up:

    image.red_mask   = 0b1111100000000000;
    image.green_mask = 0b0000011111100000;
    image.blue_mask  = 0b0000000000011111;

...but clang-format won't allow this :(

PK

Chris Blume

unread,
Jan 9, 2018, 3:39:15 AM1/9/18
to Peter Kasting, Avi Drissman, Brett Wilson, cxx
That is something about our style guide that makes me a bit sad. Vertical alignment is a tool we can't use. Oh well.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.

Mark Mentovai

unread,
Jan 9, 2018, 4:14:33 PM1/9/18
to Peter Kasting, Avi Drissman, Brett Wilson, cxx
Peter Kasting wrote:

I thought about arguing for digit separators, but worried that they could increase the risk of editor incompatibility.

I can’t see using editor compatibility as a guiding light, particularly not for a language feature that would be used so sparingly.

It's also not totally clear where one puts the separator in a binary literal (every byte? every nybble?).

The appropriate choice will vary depending on context. It’s best not to be prescriptive.

Peter Kasting

unread,
Jan 9, 2018, 4:16:47 PM1/9/18
to Mark Mentovai, Avi Drissman, Brett Wilson, cxx
It seems like there's a lot of interest in the question of separators, so I'm going to spin up a separate CL + discussion thread for those so we can link clearly to each in the notes column for each feature in the table.

PK

Jeremy Roman

unread,
Jan 12, 2018, 10:16:41 AM1/12/18
to Peter Kasting, Mark Mentovai, Avi Drissman, Brett Wilson, cxx
Binary literals seem like they have ~no downside (for the places they are better than hex literals, anyhow), so +1 to that. Discussing ' separators separately makes sense to me.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.

Peter Kasting

unread,
Jan 18, 2018, 9:54:29 PM1/18/18
to cxx
Reply all
Reply to author
Forward
0 new messages