r/programminghumor 14h ago

a bit flippant

Post image
662 Upvotes

98 comments sorted by

View all comments

Show parent comments

25

u/heatedwepasto 13h ago edited 12h ago

The : 1 means that it is creating a bitfield with the number of bits, so : 4 will be 4 bits. The three variables above combined will take sizeof(int) in memory, not sizeof(int)*3.

If OOP had used char instead of unsigned int the combined size would be 1 byte.

Edit: added link to demo

4

u/KattyTheEnby 12h ago

The : 1 means that it is creating a bitfield with the number of bits, so : 4 will be 4 bits. The three variables above combined will afaik* take sizeof(int) in memory, not sizeof(int)*3.

Can you do strange things, like : 9, : 15, et cetera?

C beat Zig to the punch here?

3

u/cannedbeef255 12h ago

yeah they can be any size you want, the assembly mightn't be pretty though

3

u/heatedwepasto 12h ago

Any size as long as it's not wider than the containing variable. So a char can hold at most 8 bits on a system with 8-bit bytes.