c - How to fill bitfields with 0xff -
is there way initialize unsigned field in bitfield 0xfff...(according its' size ofcourse) ? if use -1 warning assigning signed unsigned variable.
one solution manually create init values in enum , use them:
enum init_bit { // etc i_3 = 7u, // use longer names because global space i_4 = 15u, i_5 = 31u, // etc };
you write once , becomes easy:
x x = {/*other fields*/, i_4, /*other fields*/};
another solution found init 0
, decrement. defined because dealing unsigned type. if need initialize can create function this:
x get_x() { x x; x.a = 0; --x.a; return x; }
with optimizations enable compiler direct initialize:
get_x: movl $15, %eax ret
you can use function initialization:
x x = get_x();
of course has disadvantage need init other fields of x
in get_x
.
where x
defined as:
struct x { unsigned : 4; }; typedef struct x x;
it seems tricky initialize bit fields without warnings (gcc 6.1):
x x = {-1}; //!!warning: negative integer implicitly converted unsigned type [-wsign-conversion] x x = {-1u}; //!!warning: large integer implicitly truncated unsigned type [-woverflow] x x = {(unsigned)-1}; //!!warning: large integer implicitly truncated unsigned type [-woverflow] x x = {~0}; //!!warning: negative integer implicitly converted unsigned type [-wsign-conversion] x x = {~0u}; //!!warning: large integer implicitly truncated unsigned type [-woverflow]
Comments
Post a Comment