3 ms·
Interesting stuff. I have been a C coder (I'm not so much any more) for about 12 years of my career, and never came across bitfields in all that time, even whe
by Nursie 9y ago
Interesting stuff.
I have been a C coder (I'm not so much any more) for about 12 years of my career, and never came across bitfields in all that time, even when doing embedded work.
One style question - why not use the stdint.h types all the way through?
- makmanalp 9y ago> I have been a C coder (I'm not so much any more) for about 12 years of my career, and never came across bitfields in all that time, even when doing embedded work These days it shows up in databases stuff, either because you have e.g. one of these flags per row in your database (or imagine keeping track of used/unused fields in an in-memory block of data), which might add up to millions of booleans, and packing them by a ratio of 64 is a big improvement in space and also time it takes to process that data because the denser the data is, the more of it you can read per second and the more of it can fit in CPU caches. Also you can then use SIMD instructions to process it quicker too, etc etc.
- keithnz 9y agoweird.... most embedded platforms do something like ( following from an actual lib header, which is full of the same ). The entire interface to the hardware is basically bitfields. typedef union { struct { unsigned REC :8; }; struct { unsigned REC0 :1; unsigned REC1 :1; unsigned REC2 :1; unsigned REC3 :1; unsigned REC4 :1; unsigned REC5 :1; unsigned REC6 :1; unsigned REC7 :1; }; } RXERRCNTbits_t;
- eecsninja 9y agoDid you forget to include field width specifiers?
- jschwartzi 9y agoI've seen this a lot in Texas Instruments' bare-metal BSPs for their processors that are generated using HalCoGen. Their own proprietary compiler handles them as you'd expect. Other BSPs typically follow the coding style for their operating system. I've never seen QNX BSP drivers use bitfields. I've also never seen them in Linux drivers. QNX and Linux typically use variants of GCC to compile their drivers unless you pay for another toolchain.
- khedoros1 9y agoI've been a C++ programmer for almost as long, with some work in C. I discovered bitfields while writing emulators, in particular, building representations of registers. I'd suspect that if you haven't run into them in embedded work, that maybe they used macros and bitmasking instead.
- Nursie 9y agoI believe you are correct. Lots of that.
- jandrese 9y agoOne place you can come across them is in IP header processing. Very first byte of an IPv4 header is defined as: struct ip { #if BYTE_ORDER == LITTLE_ENDIAN u_char ip_hl:4, /* header length */ ip_v:4; /* version */ #endif #if BYTE_ORDER == BIG_ENDIAN u_char ip_v:4, /* version */ ip_hl:4; /* header length */ #endif As you can already see, they're kind of a pain to work with, so most people avoid them when possible.