I am trying to set the most significant bit in a long long unsigned, x. To do that I am using this line of code:
x |= 1<<((sizeof(x)*8)-1);
I thought this should work, because sizeof gives size in bytes, so I multiplied by 8 and subtract one to set the final bit. Whenever I do that, the compiler has this warning: "warning: left shift count >= width of type"
I don't understand why this error is occurring.
The 1
that you are shifting is a constant of type int
, which means that you are shifting an int
value by sizeof(unsigned long long) * 8) - 1
bits. This shift can easily be more than the width of int
, which is apparently what happened in your case.
If you want to obtain some bit-mask mask of unsigned long long
type, you should start with an initial bit-mask of unsigned long long
type, not of int
type.
1ull << (sizeof(x) * CHAR_BIT) - 1
An arguably better way to build the same mask would be
~(-1ull >> 1)
or
~(~0ull >> 1)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments