I've got a book on order to learn about C, but I was hoping you guys could help me in the mean time.
I'm looking at microchipc.com FAQs for some help, and I came across this which I don't understand.
This supposedly turns on and off one bit at a time:
unsigned char x=0b0001;
bit_set(x,3); //now x=0b1001;
bit_clr(x,0); //now x=0b1000;*/
#define bit_set(var,bitno) ((var) |= 1 << (bitno))
#define bit_clr(var,bitno) ((var) &= ~(1 << (bitno)))
This apparently turns on and off multiple bits:
unsigned char x=0b1010;
bits_on(x,0b0001); //now x=0b1011
bits_off(x,0b0011); //now x=0b1000 */
#define bits_on(var,mask) var |= mask
#define bits_off(var,mask) var &= ~0 ^ mask
Can some one please break it down Barney style for me? Some of the operators are throwing me off. Furthermore, I don't get why it's defined AFTER the command. I don't really understand anything about C, but I thought definitions had to be made before the commands.
I'm looking at microchipc.com FAQs for some help, and I came across this which I don't understand.
This supposedly turns on and off one bit at a time:
unsigned char x=0b0001;
bit_set(x,3); //now x=0b1001;
bit_clr(x,0); //now x=0b1000;*/
#define bit_set(var,bitno) ((var) |= 1 << (bitno))
#define bit_clr(var,bitno) ((var) &= ~(1 << (bitno)))
This apparently turns on and off multiple bits:
unsigned char x=0b1010;
bits_on(x,0b0001); //now x=0b1011
bits_off(x,0b0011); //now x=0b1000 */
#define bits_on(var,mask) var |= mask
#define bits_off(var,mask) var &= ~0 ^ mask
Can some one please break it down Barney style for me? Some of the operators are throwing me off. Furthermore, I don't get why it's defined AFTER the command. I don't really understand anything about C, but I thought definitions had to be made before the commands.