Skip to content

Instantly share code, notes, and snippets.

@cacharle
Created June 28, 2025 11:32
Show Gist options
  • Save cacharle/d224475c2f50fe7c029f8e65b1c3fb81 to your computer and use it in GitHub Desktop.
Save cacharle/d224475c2f50fe7c029f8e65b1c3fb81 to your computer and use it in GitHub Desktop.
bit_field_minimum_size
#include <stdio.h>
struct withshort
{
short c : 4;
short c2 : 4;
short c3 : 1;
short c4 : 7;
};
struct withint
{
int c : 4;
int c2 : 4;
int c3 : 1;
int c4 : 7;
};
struct withlong
{
long c : 4;
long c2 : 4;
long c3 : 1;
long c4 : 7;
};
int main()
{
printf("withshort size = %zu\n", sizeof(struct withshort));
printf("withint size = %zu\n", sizeof(struct withint));
printf("withlong size = %zu\n", sizeof(struct withlong));
return 0;
}
withshort size = 2
withint size = 4
withlong size = 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment