My Tech notes: Useful C #define Functions - set clear bit
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Wednesday, August 27, 2008

Useful C #define Functions - set clear bit

There are many useful C #define function, as an embedded software engineer, one need to know about some handy #define function. Below is list of some of the #defines which are being used

1. clear bit
#define clear_bit(val, bit) (val &= (~(1<<.bit))) usage: clear_bit(port0,0); clear zero bit of input is unsigned int
2. set bit

#define set_bit(val, bit) (val |= (1<<.bit)) usage: set_bit(port0,0); set zero bit of input is unsigned int

3. Error handling

#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
Usage:
if opening a file is failed like
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1)
FATAL;

No comments:

Post a Comment