This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

understanding the syntax of MACROs setbit, clearbit

I want to create a fctn which can set or clear a bit based on the value of a variable.

I attempted to incorporate the provided macro fctns; it didn't work. It compiled, but didn't change port state. However, I don't understand the syntax of the macro.
Q: why doesn't my fctn work?
Q: how can the macro fctn work:
using a keyword 'bit' for a variable name, and not defining a variable 'var'?

Example code snippets below:
... in includes.h
#define setbit(var,bit) (var |= (0x01 << (bit)))
#define clrbit(var,bit) (var &= (~(0x01 << (bit))))

... in main.c
void changebit( UINT16 var, UINT8 bitn, bit bitstate) {
if (bitstate) setbit(var,bitn);
else clrbit(var,bitn);
}

... in main()
changebit(P7,4,bitstate);

Thank you.
David

0