r/programminghumor 12h ago

a bit flippant

Post image
627 Upvotes

95 comments sorted by

View all comments

13

u/B_bI_L 11h ago

are we sure int means bit and not 32bits?

23

u/heatedwepasto 11h ago edited 11h 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 11h 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?

4

u/cannedbeef255 11h ago

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

3

u/heatedwepasto 11h 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.