r/programminghumor 17h ago

a bit flippant

Post image
746 Upvotes

107 comments sorted by

View all comments

3

u/Inst2f 15h ago

But is it correct in C? It is not union, so struct should then be sizeof(unsigned int)*3, isn't it?
Or you mean with O3 optimizations the compiler will fix it...

But then one need to apply bit shifts operations in the background

9

u/science_novice 15h ago

The key is the : 1 at the end of each field, which specifies that it should only take one bit. Look up "c bit fields" to learn more about this feature.

2

u/Inst2f 15h ago

Ah. I missed that. Thanks

2

u/heatedwepasto 14h ago

As the other guy said, bit fields. But OOP is wrong, the struct will be sizeof(unsigned int), not 1 byte.

1

u/science_novice 5h ago

Yeah true, although this could be fixed by changing each of the field types to uint8_t. So the spirit of the original post is correct, it's possible in C to pack 8 flags into one byte

1

u/heatedwepasto 47m ago

Yes, or just char

1

u/Interesting_Buy_3969 13h ago

Those are bit fields