Embedded systems and IoT

Stage 1 · Lesson 3 of 7

Bitwise operations and masks

C for embedded

are controlled by , and each bit or small group of bits in a register means something: enable the clock, set a pin as output, choose a speed. Bitwise operators let you change one bit without disturbing the others. OR with a mask sets bits, AND with an inverted mask them, XOR toggles them, and AND tests whether they are set.

Shifts build the mask, as in 1U << 5 for bit 5, and extract fields, as in (reg >> 4) & 0x0F for a 4-bit field. Practise reading hexadecimal (base 16) quickly, because datasheets write masks in hex.

Common trapwriting reg = mask when you meant reg |= mask, which wipes every other setting in the register and produces a bug that looks like a hardware fault.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Keep handy

Also worth your time

Mark this stage as not done?