index
is of type int
, which is probably 32 bits on your system. Shifting a value by an amount greater than or equal to the number of bits in its type has undefined behavior.
Change index
to unsigned long
(bit-shifting signed types is ill-advised). Or you can change 1<<index
to 1L << index
, or even 1LL << index
.
As others have pointed out, test
is uninitialized. You can initialize it to all zeros like this:
struct foo test = { 0 };
The correct printf
format for size_t
is %zu
, not %ld
.
And it wouldn't be a bad idea to modify your code so it doesn't depend on the non-portable assumption that long
is 64 bits. It can be as narrow as 32 bits. Consider using the uint_N_t
types defined in <stdint.h>
.
I should also mention that bit fields of types other than int
, unsigned int
, signed int
, and _Bool
(or bool
) are implementation-defined.