8
  • Is bitfield a C concept or C++?

  • Can it be used only within a structure? What are the other places we can use them?

  • AFAIK, bitfields are special structure variables that occupy the memory only for specified no. of bits. It is useful in saving memory and nothing else. Am I correct?

I coded a small program to understand the usage of bitfields - But, I think it is not working as expected. I expect the size of the below structure to be 1+4+2 = 7 bytes (considering the size of unsigned int is 4 bytes on my machine), But to my surprise it turns out to be 12 bytes (4+4+4). Can anyone let me know why?

#include <stdio.h>

struct s{
unsigned int a:1;
unsigned int b;
unsigned int c:2;
};

int main()
{
  printf("sizeof struct s = %d bytes \n",sizeof(struct s));
  return 0;
}

OUTPUT:

sizeof struct s = 12 bytes 

5 Answers 5

12

Because a and c are not contiguous, they each reserve a full int's worth of memory space. If you move a and c together, the size of the struct becomes 8 bytes.

Moreover, you are telling the compiler that you want a to occupy only 1 bit, not 1 byte. So even though a and c next to each other should occupy only 3 bits total (still under a single byte), the combination of a and c still become word-aligned in memory on your 32-bit machine, hence occupying a full 4 bytes in addition to the int b.

Similarly, you would find that

struct s{
unsigned int b;
short s1;
short s2;
};

occupies 8 bytes, while

struct s{
short s1;
unsigned int b;
short s2;
};

occupies 12 bytes because in the latter case, the two shorts each sit in their own 32-bit alignment.

Sign up to request clarification or add additional context in comments.

Comments

8

1) They originated in C, but are part of C++ too, unfortunately.

2) Yes, or within a class in C++.

3) As well as saving memory, they can be used for some forms of bit twiddling. However, both memory saving and twiddling are inherently implementation dependent - if you want to write portable software, avoid bit fields.

5 Comments

To the downvoter - which of these statements do you disagree with?
+1: 'implementation dependent' and 'avoid bitfields' are key.
I'll partially disagree here with respect to memory savings: feel free to specify bit fields where they might reduce the memory footprint of a structure. But never try to be clever with them and seven times never count on what the compiler will do with them.
As long as the base type of the bitfield is an unsigned type, the arithmetic properties of the bitfield are well-defined and the same everywhere. For example, if you have an unsigned x : 4; bitfield, then calculations in x are always done modulo 16.
In embedded code bit fields are often vital for writing efficient code e.g. toggling bits in a memory mapped output port. The port is defined as a bit field and individual port pins are controlled by setting the bit value to 1 or 0. This makes the code easily maintainable. Hardware port allocations can be modified in 1 place in the code. If the processor has bit manipulation instructions (many do) then the compiler is likely to use these. The alternative is to use masks with an AND/OR operation. This is harder to maintain, less readable and likely to produce less efficient code.
0

Its C.

Your comiler has rounded the memory allocation to 12 bytes for alignment purposes. Most computer memory syubsystems can't handle byte addressing.

Comments

0

Your program is working exactly as I'd expect. The compiler allocates adjacent bitfields into the same memory word, but yours are separated by a non-bitfield.

Move the bitfields next to each other and you'll probably get 8, which is the size of two ints on your machine. The bitfields would be packed into one int. This is compiler specific, however.

Bitfields are useful for saving space, but not much else.

Comments

0

Bitfields are widely used in firmware to map different fields in registers. This save a lot of manual bitwise operations which would have been necessary to read / write fields without it. One disadvantage is you can't take address of bitfields.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.