Binary literals?

Hello.

I’m making a module that intends to store some preset “patterns” in an array using binary literals, e.g.

static const int patterns[8][16] = {
	// Preset pattern 1
	{
		0b0000,
		0b0001,
		0b0010,
		0b0011,
		0b0100,
		0b0101,
		0b0110,
		0b0111,
		0b1000,
		0b1001,
		0b1010,
		0b1011,
		0b1100,
		0b1101,
		0b1110,
		0b1111
	},
	// Preset pattern 2
	// …

Reading around on the web, I’m unclear if binary literals are part of the C++11 or C++14 standard. I’m also reading that binary literals are supported by compilers (like g++) anyway, regardless of the 11 / 14 standard confusion.

A test C++ program which used binary literals compiled fine in g++, but given that I see the compiler flag -std=c++11 a lot when building my module with the Rack SDK, I’m not clear if I should be using them.

Any advice would be greatly appreciated.

Apologies if this has a simple answer but my C++ skills are in development. :slight_smile:

Thanks,
Alan.

Binary literals with 0b or 0B were introduced in C++14, so they are not valid C++11. You could probably make an implementation with user-defined literal suffixes like https://stackoverflow.com/a/538101/272642, or with string literals and a conversion function like int binaryString(std::string s).

2 Likes

Thanks, Andrew!