Friday, April 27, 2012

Set and clear bit using MACRO


#include "<"stdio.h">"
#define CLRBIT(byte,_bit) byte&=~(1<<_bit)//clear bit in a byte _bit is the position want to clear
#define SETBIT(byte,_bit) byte|=(1<<_bit)
main()
{ unsigned char byte=0xff;
printf("\n The value before clear=0x%x",byte);
byte=CLRBIT(byte,1);
printf("\n The value after clear=0x%x",byte);
byte=SETBIT(byte,1);
printf("\n The value after set=0x%x",byte);
} //output
//The value before clear=0xff;
//The value after clear=0xfd;
//The value after set=0xff;

No comments:

Post a Comment