can you read or write 8 bits at once?

My preference is to use a "bit" macro that defines a constant for an individual bit
then OR them together to create other constants.
I also like to use individual defines for each bit because usually the bits mean something
and you can assign them mnemonic names for clarity.

It makes the code easier to read and much less prone to errors since when typing in
binary constants it is easy to miscount the bits and create incorrect constants.

For example all these assignements to "mask" are the same
yet at least in my in my view the last one is the easiest to tell what
is going on:

#define BIT(x) (1 <<x)
#define RED BIT(0)
#define GREEN BIT(1)
#define BLUE BIT(2)
#define YELLOW (RED |  GREEN)
#define LED_ON BIT(15)

const int mask = 0b1000000000000011;
const int mask = BIT(15) | BIT(1) | BIT(0);
const int mask = LED_ON | GREEN | RED;
const int mask = LED_ON | YELLOW;

Note: The AVR tools define _BV() the same as BIT() above
but it is non standard. If you define it yourself, then the code
will work on any C compiler.

--- bill


floresta:

PORTC = B00010101; //set bits 0,2,4 to a 1, the rest to 0.

For some unfathomable reason this is not possible with some C compilers but as far as I know it works ok with the Arduino.

Don

This is not a C compiler issue but rather a self created issue by
team Wiring/Arduino

All C compilers support binary constants.
The problem is what I can only describe as what appears to be a NIH (not invented here) mindset in team Wiring/Arduino
which often decides that having a similar yet proprietary way of doing things
is better than simply explaining the real way of doing things in the language being used: C/C++
In this case the team Wiring/Arduino guys invented their own binary constants.
This kind of deliberate duplication of functionality
has always frustrated and annoyed me particularly because just like in this
case it isn't a complete replacement for the built in solution.

I mean why not simply explain that the binary syntax is

0bxxxxxxxx

How is that any harder than
Bxxxxxxxx

Which is non standard, requires a custom header file and
a define for every single Bxxxxxxxx define?
Today's Arduino solution only works for 8 bit values.
Take a look at binary.h down in the core directory
sometime. It is comical and yet sad at the same time.

The method they have used does not scale beyond 8 bits.

Think of the size of that header file if it needs to support
16 bit numbers or the impossibility of using it for
32 bit binary constants for something like DUE?