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!

Further to this, I have written code which uses an array of boolean integer values to store the “bits” for the patterns in my beta Bitwise module (see https://github.com/chortlinghamster/modules/blob/5632f9c91e33663c5dac0da2b04e9d7b3e6dde4e/src/Bitwise.cpp#L59).

I’ve been reading about std::bitset in C++. Would doing something like the following code be supported in a module built using the Rack SDK?

#include <bitset>

// ...

// Current version of code
int patterns[16][64] = {
	{0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,1,0,1,1,0,0,1,1,1,1,0,0,0,1,0,0,1,1,0,1,0,1,0,1,1,1,1,0,0,1,1,0,1,1,1,1,0,1,1,1,1},
	{0,0,0,0,0,0,0,1,0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,1,1,0,1,1,1,1,0,1,1,1,0,0,1,1,0,0,0,1},
	// ...

// Better? Not sure if supported in Rack SDK.
std::bitset<64> patterns[16] = {
	(std::bitset<64>)("0000000100100011010001010110011110001001101010111100110111101111"),
	(std::bitset<64>)("0000000100110111111111101100100000001000110011101111011100110001"),
	// ...

If I could use std::bitset I’m not sure it if might improve the performance of checking the bits in the patterns, using something like std::bitset<N>::test.

I apologise for the noob question but I’m still learning C++. Many thanks for any advice given.

std::bitset is perfectly fine if that’s your preferred data structure for your binary data.

Thanks very much, Andrew.