Saturday, August 23, 2014

Bit Twiddling Reference for AVR programmers

If you do any AVR programming in c or c++, you'll very likely need these operations. As often as I've seen them, I still sometimes need to look these up if I haven't used them in a while. So here they are...

Assume that the variable myByte is a 8 bit variable defined as the following in c code:
                                                                  byte myByte = 0x00;

The above line of code sets all 8 bits (0-7) to 0, turning them all OFF using the hex notation.

Set bit  i (ON/1):                          myByte |= (1 << );

Clear bit  (OFF/0):                    myByte &= ~(1 << );

Toggle bit                                myByte ^= (1 << );