Notifications
Clear all

[Solved] Extended Arduino bit operator macros

1 Posts
1 Users
0 Reactions
1,314 Views
ron bentley
(@ronbentley1)
Member
Joined: 3 years ago
Posts: 385
Topic starter  

Arduino Bit Operator Macros

I was reviewing some old work recently and came across a set of Arduino bit operator macros that I modified at one time when I was exploring the use of 64 bit unsigned variables to record bit maps for multiple 74HC595 shift registers.  Long story short, I eventually chose an 8-bit map design and discarded the extended 64-bit macro operators.

However, these modified bit operator macros work well for any variable up to and including 64 bits, i.e. 8-bits, 16-bits, 32-bits and 64-bits. 

I thought it would be of interest to share these with forum members for interest and, perhaps, use. Either way, it may be worthwhile sticking them in your tool box for a future use?

The standard Arduino bit operator macros work fine for 8, 16 and 32 bit variables. But, these macros can be easily modified and extended to also deal with 64 bit variables as we can see below.

The standard Arduino bit operator macros are:

#define bitRead(value,  bit) (((value) >> (bit)) & 0x01)
#define bitSet(value,   bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value,  bit))

 

The modified and extended bit operator macros are:

#define bitRead(value,  bit) (((value) >> (bit)) & 1ULL)
#define bitSet(value,   bit) ((value) |=  (1ULL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1ULL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value,  bit))

 

Notice the difference?  The only difference is that the bit mask value (1) is forced to take an unsigned 64 bit data type – Unsigned Long Long (ULL).

Okay, I hear you say, how do I know it actually works?  Well, before I set down to write this post I put together a simple sketch to confirm the correct operation of each of the bit operator macros – Read, Set, Clear and Write.  See the sketch:

 

 

The sketch considers four unsigned variables of bit lengths 8, 16, 32 and 64 and then, for each, runs through an exercise to set/clear and read each bit of each variable, showing the incremental results to the serial monitor.  The sketch carries out two passes – the first pass sets all bits in all variables incrementally and a second pass to clear all bits, again incrementally.

If you are wondering what would happen if the extended bit operator macros are not used, but the standard Arduino ones instead, then edit the sketch to comment out the #defines for the four extended bit operator macros and run it.

What do you notice?  Well, they work just fine for variables of bit length 8, 16 and 32, but not 64.  Not a surprise really, but nice to have the confirmation.

I was uncertain where to plant this post – Arduino, C++ or Show & Tell.  I went for C++, but it is equally a topic for all three categories!

Anyway, I hope you found this post of interest.

 

Ron B

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


   
Quote