Re: How to use bitfields?



On Feb 18, 8:24 am, Radamanthe <tek...@xxxxxxxxxxxxxxxx> wrote:
cman wrote:

What are the advantages of using bitfields?

Memory usage, essentially, at the very lowest possible level. Note that
we could always do without bitfields anyway (by using bitwise operators
like & and |). It's just for readability that bitfields are used.

They are often used to provide an intuitive interface to an underlying
IO register or low-level structure. This way, you use the same semantic
to access values as any other type. Consider, as an example, a pixel
definition in some 16 bits ARGB format:

#include <stdio.h>
#include <stdint.h> // C99 uint16_t

struct ARGB1555 {
union {
uint16_t value;
struct {
unsigned int blue : 5;
unsigned int green : 5;
unsigned int red : 5;
unsigned int alpha : 1;
} comp;
} u;

};

int main(void)
{
struct ARGB1555 pixel;
pixel.u.comp.red = 20;
pixel.u.comp.green = 22;
pixel.u.comp.blue = 7;
pixel.u.comp.alpha = 1;
printf( "red: %d, green: %d, blue: %d, alpha: %d\n",
pixel.u.comp.red,
pixel.u.comp.green,
pixel.u.comp.blue,
pixel.u.comp.alpha );

return 0;

}

This is far more readable than this uggly equivalent machine dependant crap:

int main(void)
{
struct ARGB1555 pixel;
pixel.u.value = (1 << 15) | (20 << 10) | (22 << 5) | 7;
printf( "red: %d, green: %d, blue: %d, alpha: %d\n",
(pixel.u.value >> 10) & 0x1f,
(pixel.u.value >> 5) & 0x1f,
pixel.u.value & 0x1f,
(pixel.u.value >> 15) & 0x1 );

return 0;

}

Why do you describe the second version as "machine dependent crap"?
Your use of bitfields in the first example is entirely implementation
dependent, since very little about the layout of bitfields is defined
in C. One compiler may lay them out from the most significant bit
down, another from the least significant up, for example. If you do
this with bitfields, you may need to re-implement your code for each
different compiler or target.

The second method, however, is implementation and machine independent
and entirely portable (in principle - I've not checked the details of
your example). Code written in this way is portable between compilers
for the same target, and often across different targets.

.



Relevant Pages

  • Re: Variable declaration followed by colon?
    ... A couple of common examples where bitfields are used are device drivers, ... you're relying on bitfields every time you send an IP packet! ... Here's the IP header struct that's used in every packet: ... unsigned int version:4; ...
    (comp.lang.c)
  • Re: How to use bitfields?
    ... difference with other integer types beside their bit size. ... It's just for readability that bitfields are used. ... struct ARGB1555 { ... unsigned int green: 5; ...
    (comp.lang.c)
  • Re: How to use bitfields?
    ... It's just for readability that bitfields are used. ... struct ARGB1555 { ... unsigned int green: 5; ... I don't use C because of its portability, but because it is the only language that allow me to do pseudo-assembly low-level code very tied to the hardware in such a way that I don't have to rewrite everything for each target. ...
    (comp.lang.c)
  • Re: When shorts are longer than longs !
    ... the bitfields have such annoyances (especially ... unsigned int BitsInUnsignedInt ... unsigned int BaseBit) ...
    (comp.lang.c)
  • Re: Checking endianess in compile time
    ... Bitfields *ARE* regarded properly by the compiler. ... > unsigned int tffca: 1; ... tells you the endianness. ...
    (comp.lang.c)