#Bitfields vs Bit-Packed Structures (Manual Bitmasking) in C?

1 messages · Page 1 of 1 (latest)

frail trellisBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

frail trellisBOT
#

@edgy lynx

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

south crystal
#

Only difference I understood is the padding management and readability, especially readability.
I'm reading now that if you set a field to zero bits, you force the next field to be aligned to a new storage unit.
Padding is annoying with bitfields. The position of your fields is not guaranteed, thus the example above. You kinda have to force it.

#

This can be an issue if you plan on transferring your stuff over to a different endianness, since you don't directly control the way bits will be packed.

#

Whereas with manual bitpacking you have full control.

#

Just a little more verbose. I use only manual bitpacking.

#

I'm reading that another concern can be when interaction with low-level components that require a precise bit position format. Clearly, if your structure does not resemble the expected one you get weird results.

#

It's the compiler deciding how to place your bits in a bitfield.

#

Also, to close the question say !closed, don't delete it.

#

FOr anyone wondering who asked, no one did. Literally. @edgy lynx

frail trellisBOT
# frail trellis

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

edgy lynx
south crystal
#

Don't do that again.

#

It was a nice question I learnt from.

edgy lynx
muted fable
#

guessing the question: if your goal is just reducing memory usage for some arrays, then the built-in syntax in struct; if you are working with binary data, like files, hardware registers, etc, where the layout matters - then manual operations