You can just shift the value up into the unused bits, e.g.
To store the value:
mask |= val << 28;
To retrieve the value:
val = mask >> 28;
Note that mask
should really be unsigned
to avoid propagating the sign bit when shifting. If you have to use a signed int for some reason then you should add an additional masking operation when retrieving val
, e.g.
val = (mask >> 28) & 0x0f;