The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C BitFields

Bit fields provides exact amount of bits required by the variable. Bit fields uses the memory very efficiently. The bits must be specified by non-negative(unsigned) integer type followed by a colon(:). Bit fields can also used as member in unions.

C program - Bit Fields

Lets code Bit Fields and have some fun.

bitfields.c
#include <stdio.h>
int main()
{
struct sample {
unsigned int b1;
unsigned int b2;
}s1;
struct sample2 {
unsigned int b1:1;
unsigned int b2:1;
}s2;
printf("Size of structure sample : %d ", sizeof(s1));
printf("\nSize of structure sample2 : %d ", sizeof(s2));
return 0;
}
  • Size of structure sample : 8
  • Size of structure sample2 : 4

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report