Bit Manipulation & Register Fields
and/orr/eor/bic, shifts, bit-field insert and extract, counting bits, and a CRC in assembly.
Peripheral registers, protocol headers and checksums are all about bits. ARM has rich bitwise instructions, including single instructions that insert or extract a whole bit field — operations that take several lines of C.
Bits in practice
bic (bit clear) computes a AND NOT b: clearing the field before inserting the new value.
bfi w0, w1, #10, #2 inserts the low 2 bits of w1 into bits 11:10 of w0 — setting STM32 pin 5 to output mode in one instruction.
ubfx extracts an unsigned bit field (here bits 11:8).
x & (x - 1) clears the lowest set bit, so the loop counts set bits.
crc8 implements CRC-8/SMBUS; 0xF4 is its published check value.
Notes
orr w0, w0, w1, lsl #10 // to write a 2-bit field clear the field first (bic, or use bfi)
OR can only set bits; if the field already held 10 and you write 01, the result is 11. Clear, then set — or use bfi.
Which single instruction extracts bits 15:12 of w1 into w0?
ubfx w0, w1, #12, #4