Safe Bitfields in C++

from blog Preshing on Programming, | ↗ original
In my cpp11-on-multicore project on GitHub, there’s a class that packs three 10-bit values into a 32-bit integer. I could have implemented it using traditional bitfields… struct Status { uint32_t readers : 10; uint32_t waitToRead : 10; uint32_t writers : 10; }; Or with some bit twiddling… uint32_t status = readers | (waitToRead ...