bit manipulation - set most significant bit in C -
i trying set significant bit in long long unsigned, x. using line of code:
x |= 1<<((sizeof(x)*8)-1);
i thought should work, because sizeof gives size in bytes, multiplied 8 , subtract 1 set final bit. whenever that, compiler has warning: "warning: left shift count >= width of type"
i don't understand why error occurring.
the 1
shifting constant of type int
, means shifting int
value sizeof(unsigned long long) * 8) - 1
bits. shift can more width of int
, apparently happened in case.
if want obtain bit-mask mask of unsigned long long
type, should start initial bit-mask of unsigned long long
type, not of int
type.
1ull << (sizeof(x) * char_bit) - 1
an arguably better way build same mask be
~(-1ull >> 1)
or
~(~0ull >> 1)
Comments
Post a Comment