Accesing bit addressable variables

I was wondering what the "best" way to set a bit in a variable declared in the bit addressable area.

Originally I declared each bit as an sbit and therefore can set and clear them as follows

uint8_t bdata status ;
sbit enable = staus ^ 0 ;

enable = 1 ;
enable = 0 ;
I think it looks clearer to do as follows
#define ENABLE 0x01

uint8_t bdata status ;

status |= ENABLE ;
status &= ~ENABLE ;

but I am worried that this could less maintainable because there will be many #defines with the same values but related to different variables and confusion could ensue.

I was wondering what other people do and if there is a standard method for doing this?

Parents
  • "I was wondering what other people do and if there is a standard method for doing this?"

    The #define and masking is the conventional way when using a compiler without specific bit-addressing extensions (and for non bit-addressable data with Keil).

    Although you seem to have found a (the?) case where the Keil compiler is smart enough to replace a simple mask with a bit-set operation, the only way to guarantee it would be to use the bit-addressing extensions.

    And the current score is 2:1 that the bit-addressing is clearer! ;-)

Reply
  • "I was wondering what other people do and if there is a standard method for doing this?"

    The #define and masking is the conventional way when using a compiler without specific bit-addressing extensions (and for non bit-addressable data with Keil).

    Although you seem to have found a (the?) case where the Keil compiler is smart enough to replace a simple mask with a bit-set operation, the only way to guarantee it would be to use the bit-addressing extensions.

    And the current score is 2:1 that the bit-addressing is clearer! ;-)

Children
More questions in this forum