The output of this code is -1, 4, 4. I am not sure about this -1. Is it because when all bits are set then in decimal it means -1 ?


int main()
        struct value
                int bit1:1;
                int bit3:4;
                int bit4:4;

        printf("%d %d %d",bit.bit1, bit.bit3, bit.bit4);

change them from int to unsigned int and see what happens. Also change bit1 to be 2 bits instead of 1.

The output you see is absolutely correct. See AD's advice above. If bit1 was 2 bits instead of one, it would have output the expected answer. IE, an integer value with all bits set == -1 in decimal. That is what you have in your example. This is a good example of "what you say is not exactly what you get". :-)

Thanks for the reply :)

One final note. main() returns an int, but you have no return value... Just nit picking! :-) FWIW, the compiler should have complained.

Some compilers such as MS VC++ 2012 don't complain about that. I set it at it's highest warning/error level and it still didn't complain about no return value.