Skip to content

bitSet and bitClear macros do not work for 64 bit ints (solution provided) #251

@RobTillaart

Description

@RobTillaart

The current macros in Arduino.h for setting and clearing a bit work only for numbers 32 bits and smaller. If one wants to set a bit in a uint64_t the macro fails. The cause is the 1UL in the macros which is essentially 32 bit,

Solution:
The macros below allow setting a bit for 64bit integers too, but use the 64 bit version of 1 only if size of value is 4 bits or less.

#define bitSet64(value, bit) ((value) |= (sizeof(value)<5?1UL:1ULL) <<(bit))
#define bitClear64(value, bit) ((value) &= ~(sizeof(value)<5?1UL:1ULL) <<(bit))
#define bitWrite64(value, bit, bitvalue) (bitvalue ? bitSet64(value, bit) : bitClear64(value, bit))

It is better to test the sizeof(value) that to test bit as bit often can be a variable so its value cannot be checked compile time.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions