The size_t type is an unsigned type. As such, right-shifting a value of type size_t will shift logically. Given that the width of size_t is implementation dependent, is there any way to right-shift a size_t value arithmetically?
If my goal is to create a bitmask from a size_t value containing a 1 or a 0, is there another way to do it? For integers with known widths, the easiest way I know to make a bitmask is to left-shift the width of the integer - 1, then arithmetically right-shift all the way back.
This works on my 64-bit system:
const size_t width = (sizeof(size_t) << 3)) - 1;
size_t value = {boolean value};
value = ((int64_t) (value << width)) >> width;
But of course it's specific to my system and systems like it. What can I use instead?
Aucun commentaire:
Enregistrer un commentaire