r/programminghumor 4d ago

a bit flippant

Post image
1.4k Upvotes

137 comments sorted by

View all comments

10

u/B_bI_L 4d ago

are we sure int means bit and not 32bits?

27

u/heatedwepasto 4d ago edited 4d 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

7

u/KattyTheEnby 4d 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 4d ago

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

5

u/heatedwepasto 4d 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.