Windows System Software -- Consulting, Training, Development -- Engineering Excellence, Every Time.

Binary Literals in VS2015 — I Can’t Wait!

Binary Literals in VS2015 — I Can’t Wait!

There are a couple of features of C/C++ in Visual Studio 2015 that are easy to overlook, but that I’m really looking forward to using. Two of those features that I’m most eagerly anticipating are support for binary literals and digit separators.

Kinda boring, you say?  Hardly!

If you work with hardware, you are certainly familiar with having to create definitions of register fields for your driver header files based on the definitions in the chipset data book.  The definitions are always of the form:

FRED Register (offset 0x40):


and the like.  This process always makes me nuts.  It forces me to sit down, write the bits in binary, and convert them to hex, so they eventually look like in the following:

#define FRED_INT_CHANNEL    0x78000000
#define FRED_INT_ERRORS     0x07000000
#define FRED_INT_SOURCES    0x00003C00
#define FRED_INT_RESMBZ     0x0000FFC0
#define FRED_INT_PENDING    0x00000008
#define FRED_INT_ENABLE     0x00000001

The problem with this is (a) it’s an annoying process, and (b) the result is pretty much entirely opaque.  Once I’ve converted the definitions to hex, unless they’re very simple like the ones for FRED_INT_ENABLE and FRED_INT_PENDING in the above example, the definitions lose all intuitive meaning.  I can’t get a picture in my mind as to what these register bits look like.  Heck, I can barely tell at a glance if any of the fields I’ve defined overlap!

This is where binary literals and digit separators enter the picture.  Instead of the above hex nonsense, starting with VS 2015 you can quickly cook-up definitions like these:

#define FRED_INT_CHANNEL   0B0111'1000'0000'0000'0000'0000'0000'0000
#define FRED_INT_ERRORS    0B0000'0111'0000'0000'0000'0000'0000'0000
#define FRED_INT_RESMBZ    0B0000'0000'1111'1111'1100'0000'0000'0000
#define FRED_INT_SOURCES   0B0000'0000'0000'0000'0011'1100'0000'0000
#define FRED_INT_PENDING   0B0000'0000'0000'0000'0000'0000'0000'1000
#define FRED_INT_ENABLE    0B0000'0000'0000'0000'0000'0000'0000'0001

Ah! I don’t know about you, but to me that looks one heck of a lot more clear.  I now can see which bits are contiguous, which are adjacent, and I get a nice picture of the register’s layout.

OK, so maybe it’s a small thing.  But it’s the kind of thing that I know will make my dealings with hardware easier.