-
Notifications
You must be signed in to change notification settings - Fork 0
Bit Operations — set clear toggle
MarekBykowski edited this page May 26, 2026
·
1 revision
// Macros (Linux kernel style)
#define BIT(n) (1u << (n))
#define GENMASK(h,l) (((1u << ((h)-(l)+1))-1) << (l))
val |= (1u << n); // SET bit n
val &= ~(1u << n); // CLEAR bit n
val ^= (1u << n); // TOGGLE bit n
// SET bits 3–7 to 'mode' — raw, no macros
val = (val & ~(0x1Fu << 3)) | ((mode & 0x1Fu) << 3);
// step 1: build mask → shift to pos 3 → negate → AND (clears bits 3-7)
// step 2: shift mode to pos 3 → OR (writes new value)
// READ bits 3–7
uint32_t m = (*reg >> 3) & 0x1F;
⚠️ Always clear first, then set.|=alone cannot zero bits already set to 1.