Search code examples
cbit-manipulation

How can I clear multiple bits at once in C?


How would I simplify all of this into one line?

    REG &= ~BITA;
    REG &= ~BITB;
    REG &= ~BITC;
    REG &= ~BITD;
    REG &= ~BITE;

Solution

  • You can use | (bitwise or) operator.

    REG &= ~(BITA | BITB | BITC | BITD | BITE);