#include template constexpr T mask(size_t bits, size_t offset = 0) { return (((T)1 << bits) - 1) << offset; } template constexpr T getMasked(T data, size_t bits, size_t offset) { return (data >> offset) & mask(bits); } //TODO: support typeof(data) != typeof(value)? template constexpr T setMasked(T data, T value, size_t bits, size_t offset) { return (data & ~mask(bits, offset)) | ((value & mask(bits)) << offset); }