variable declaration: binary formatter

When declaring variables, when is the binary formatter available?

Example from documentation:

byte b = B10010;  // "B" is the binary formatter (B10010 = 18 decimal)

Is it only for single bytes? What about byte arrays, or unsigned char arrays? I ask only because it would be handy for educational purposes, and more readable in certain cases, to show the bits themselves in the code, rather than decimal numbers, characters, or 0x00 syntax. Is there some other way to declare a string of bits in binary?

The B notation is handled by the preprocessor, and only 0..255 are available. I think the "0b" notation should be OK for larger values, but I never use because by the time you get past 8 bits, you should be using hex!

When declaring variables, when is the binary formatter available?

Everywhere. The conversion of the value to a value that can be stored in a variable happens at compile time. Anywhere where a constant is expressed, the constant can be expressed in decimal, binary, octal, or hexadecimal, and the compiler will generate the proper bit pattern.

PaulS: Everywhere.

Maybe something else is going wrong. For example, if I declare an array of unsigned chars in PROGMEM:

const prog_uchar sounddata[] PROGMEM = {
B01010101, B01010101, B01010101, B01010101, B01010101, B01010101, B01010101, B01010101, B01010101, B01010101, .....

I get an error that says "B01010101 was not declared in this scope". It thinks it's supposed to be a variable name or something.

Sounds like another Arduino pre-compile problem - can you post more of your code (or learn hex)?

Ok, here’s the code. This is very much an experiment, and very much in-progress: I wanted to see if I could do DSD-style (Direct Stream Digital) 1-bit audio. Currently, the sample rate isn’t anywhere near high enough, and the memory demands of DSD-style audio make this sort of pointless, except potentially as an educational tool. (I have some PCM-based sampler code that is a little more reasonable, but still mainly a teaching tool.)

The sound data is generated by a Processing program. It may look like gibberish (and it certainly sounds like gibberish), but it is based on actual audio. I’ve also attached the source audio file (CC-BY from the One Laptop Per Child project).

This includes an ugly work-around in _binaryLookup.h. It defines PROGMEM variables like “B00000000” to avoid the errors I was getting. The get the errors, just delete the line that says "#include “_binaryLookup.h”.

I would like to get this working, but it isn’t critical. However, if this is unexpected behavior for the binary formatter, hopefully this is helpful in sorting it out. (8.5 KB)

bell.wav (10.7 KB)

85 in decimal is defined in binary.h both with and without a leading zero. I haven't looked at your sketch in detail, but it is maybe down to the order the Arduino IDE reorganises the sketch before compilation.