Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Add information about assignment operators

Understanding these three operations will make your life much easier.

Setting a Bit

The bitwise OR operator is used to set a bit. The following code snippet sets bit x.

Code Block
languagecpp
themeConfluence
extern uint8_t value;
value |= 1 << x;

Note: Using the |= assignment operator

Code Block
languagecpp
themeConfluence
value |= 1 << x;

is essentially the same as doing

Code Block
languagecpp
themeConfluence
value = value | (1 << x);

Example

Suppose we have the following value

...

Code Block
languagecpp
themeConfluence
extern uint8_t value;
value &= ~(1 << x);

Note: Using the &= assignment operator

Code Block
languagecpp
themeConfluence
value &= ~(1 << x);

is essentially the same as doing

Code Block
languagecpp
themeConfluence
value = value & ~(1 << x);

Example

Suppose we have the following value 

...

Code Block
languagecpp
themeConfluence
extern uint8_t value;
value ^= 1 << x;

Note: Using the ^= assignment operator

Code Block
languagecpp
themeConfluence
value ^= 1 << x;

is essentially the same as doing

Code Block
languagecpp
themeConfluence
value = value ^ (1 << x);

Example

Suppose we have the following value

...

Code Block
languagecpp
themeConfluence
(0b00010111 >> 2) = 0b000000010b00000101

Then, the AND operator is applied

...