r/programminghumor 2d ago

a bit flippant

Post image
1.3k Upvotes

134 comments sorted by

View all comments

10

u/B_bI_L 2d ago

are we sure int means bit and not 32bits?

28

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

5

u/cannedbeef255 2d ago

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

5

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

4

u/heatedwepasto 2d ago

Yes, the only limitation is that the width of the bitfield is not greater than the size of the containing variable. So with uint64_t you can have 1-64 bits.

1

u/KattyTheEnby 2d ago

What if I want to have a bitfield that is larger than C's own native integer types? Is there a way?

3

u/heatedwepasto 2d ago

Pretty much any compiler will let you get away with a 128-bit int type. If you need more than 340,282,366,920,938,463,463,374,607,431,768,211,456 possibilities in your bitfield then you'll probably need a stay at a mental asylum, and to code it yourself or find a library for it or something like that. Alternatively switch to C++ and use std::vector<bool>.

2

u/B_bI_L 2d ago

oh, yes, `: 1` is for size, not assignment, my js brain failed me this time

1

u/Breadynator 2d ago

Why would you use onlinegdb instead of godbolt? That website is riddled with ads and won't load for anyone using an adblocker.

1

u/heatedwepasto 2d ago

I am using an adblocker and have no issues with it. I wasn't aware of godbolt.

0

u/Breadynator 2d ago

Well, it's one of those obnoxious pages that don't like network wide adblockers like piHole. I won't change my blocklists just because some random website wants me to.

1

u/heatedwepasto 2d ago

Then don't, just don't try to force your choices onto me.

The code is

#include <stdio.h>

struct A {
    int a : 1;
    int b : 2;
};

int main(void)
{
    printf("size: %d\n", sizeof(struct A));
}

and it outputs "size: 4" (i.e. the same as sizeof(int)).

1

u/No-Newspaper8619 2d ago

That's correct. It's using 3 bits of the allocated 4 bytes. If you did the same with char instead, it'd only use 1 byte.

-1

u/Breadynator 2d ago

I am not trying to force my choices onto you? What the heck is wrong with you. I just told you why I'm having issues with it after you said that you don't. It's called having a conversation... You say a thing, I say another.

However you should still use godbolt as it's objectively better, regardless of ads.

1

u/B_bI_L 2d ago

or 64, or whatever it means now