The "Binary Formatter" and using binary literals for values >8bits

I need to define a set of 32 bit values, and the most convenient way would be to pack them into a single 32 bit integer, then read them using bitRead().

I'm quite familiar with using binary literals for 8 bit values, i.e.
uint8_t value = 0b00001000;

However, according to the documentation at
https://www.arduino.cc/en/pmwiki.php?n=Reference/IntegerConstants

The binary formatter only works on bytes (8 bits) between 0 (B0) and 255 (B11111111). If it is convenient to input an int (16 bits) in binary form you can do it a two-step procedure such as:

myInt = (B11001100 * 256) + B10101010;

At first I thought this would be a problem, but then I noticed this is only referring to the "Binary Formatter" (i.e. "B", which is processed by the pre-processor), when it seems to work absolutely fine using the standard 0b notation:

uint32_t value = 0b00001000000010000000100000001000;

So.... that got me wondering what the point of the binary formatter even is? And why does the documentation mention the workaround rather than just using 0bXXXXXXXX?

It is presumably meant to make using the binary format less intimidating for new Arduino users.

Bear in mind that the original target audience for the Arduino project was not experienced programmers

As to using binary format with 32 bit values then you may be better off using hex notation as there is less likelihood of mistakes with bit positions

Thanks for the reply - it's representing that state of a 1D array of LEDs that are either lit or unlit, so I think binary string is the clearest notation. It just seems strange to me to introduce a "helper" that duplicates the functionality of a standard C language feature, but in a non-standard and more limited way! Perhaps it's a remnant from a time when 0b literals weren't supported by the IDE/compiler?
Anyway, I shan't worry about it too much. Cheers :slight_smile:

While that's true, I think it's interesting to note that what has really pushed the envelope and made the Arduino ecosystem what it is today is not that audience but experienced technologist … both commercial and non-commercial. Companies like Adafruit, PJRC, and SparkFun. Folks who write advanced libraries like FastLED, PID, etc.

I'd say the reason for this is that, for the most part, artists and musicians don't THINK like engineers. You can see that everyday reading the forums. I'm not talking about lack of experience which is understandable, but lack of a methodical, problem-solving thought process. This includes the ability ask good questions to get good answers.

But, one place where the original target audience has really come through is providing the volume market that drives down prices for all of us. So, Cheers to them.

1 Like

Binary literals are a relatively recent advancement. At the time those macros were created they did not exist, and even now they are not available for all platforms. Even after they become available everywhere, there is still backwards compatibility with all the legacy code to consider.

I would say that if the platforms you use support binary literals and you don't plan to share the code with anyone who might be using a platform that doesn't, then there is no point in them and you should definitely use binary literals.

There is some interesting discussion on the subject here:

Thanks - that discussion does indeed seem to explain the existence of "B" as a legacy helper macro (would be nice if the documentation page I listed in my first post was updated to make reference to "0b...." as a preferred solution on supported platforms)

A bit of time programming and you may find you can see the bits just fine using hexadecimal notation.

Time to worry, maybe, when you dream in hex.

a7

It was done 1.5 years ago:

That is the source of this page in the Arduino Language Reference:

I wasn't able to to open the link you shared, even though I tried it on Chrome and FireFox. My guess is it is some ancient zombie version of the page from many years ago. I can see it is from the old "pmwiki" CMS. The language reference was migrated from that to being generated from the AsciiDoc-based content in the GitHub repository around 5 years ago.

Well, that's one of the top links that Google presents me after a search for "Arduino Binary Literal", and it doesn't mention anything about 0b.... it literally only says the text I pasted in my first post and the workaround for using B for > 8bit values by shifting * 256.

Another of Google's top hits goes to https://www.arduino.cc/en/Reference.IntegerConstants which gives a 404, so either there's something wrong with the Google cache, or the Arduino documentation server itself is serving up old/broken versions of these pages.

The exact dates are:

  • Bxxx macros introduced in Arduino 0007, released 2006-12-25
  • 0bxxx format introduced in GCC 4.3.0, released 2008-03-05
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.