4 ms·
Yes, that's straightforward at least when coming to C from assembly (as it used to be the case once upon a time...) In practice, though, bitfields are seldom us
by mytailorisrich 7y ago
Yes, that's straightforward at least when coming to C from assembly (as it used to be the case once upon a time...) In practice, though, bitfields are seldom used, so everything related to them is largely academic.
- wahern 7y agoBitfields aren't commonly used, especially in public interfaces. But usage is far more prevalent than for typical "largely academic" features. For example, $ grep -rE ':[[:digit:]][[:digit:]]*;' /usr/include/ | wc -l 702 $ uname -a OpenBSD orville.25thandClement.com 6.6 GENERIC.MP#3 amd64 $ grep -rE ':[[:digit:]][[:digit:]]*;' /usr/include/ | wc -l 276 $ uname -a Linux alpine-3-10 4.9.65-1-hardened #2-Alpine SMP Mon Nov 27 15:36:10 GMT 2017 x86_64 Linux $ grep -rE ':[[:digit:]][[:digit:]]*;' /usr/include/ | wc -l 532 $ uname -a Linux splunk0 5.0.0-36-generic #39-Ubuntu SMP Tue Nov 12 09:46:06 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
- dtornabene 7y agothey're also more commonly used in security tooling
- deleted 7y ago[deleted]
- grawprog 7y agoNot C but I used bitfields pretty extensively for the toy 24-bit virtual machine I wrote in D. Internally, it's how I represented registers, bytes and words. There's only 16MB available for addressing so I wanted individual bits to be accessible. It was the easiest way I found to do that.
- mytailorisrich 7y agoActual bitfields are used extensively. I am obviously talking about the 'bitfield' C feature, which isn't.
- grawprog 7y agoD's are fairly similar to C's bitfields. They're more portable though as far as I know because you can specify the endianness as opposed to C which uses the platform's endianness. Which from what I remember is the main reason C's bitfields are a poor choice for things. Any code isn't portable to architectures with a different endianness.
- mytailorisrich 7y agoYes, we're discussing C's bitfields...
- BubRoss 7y agoLargely academic? That is just not true in any sense. Bitfields are extremely important in many cases, not the least of which is making lock free data structures where you need to pack in information that can be atomically swapped.
- mytailorisrich 7y agoI have been developing professionally in C on embedded systems for 20 years and never had to interact with the 'bitfield' C feature. In fact, many internal coding standards forbid its use because it is poorly defined and compiler-dependent. Moreover, it does not guarantee atomicity so if atomicity is critical then you want to access the actual bitfield 'manually'.
- BubRoss 7y agoWhen dealing with lock free algorithms the amount of bits that you can do a compare and swap on are limited. For x64 it is 64 bits unaligned or 128 bits aligned. Needing to use all of these bits as efficiency as possible is common and bitfields are a big part of this. Whether you use bitfields frequently in your work is irrelevant.
- mytailorisrich 7y agoI think you're still missing the point that we're specifically discussing the 'bit field' C language feature, not bit fields in general. I do use bit fields, everyone does at one point or another when working on embedded systems. But commonly this is done 'manually' (with masks and shifts) without using C's support for bit fields.
- BubRoss 7y agoI would rather let the compiler do that. If that's what you were talking about, why did you say 'bitfields aren't atomic' ?
- souprock 7y agoThey are very common and useful for emulators. They are indeed trouble for super-portable interfaces. They also tempt some people into trouble with hardware drivers, but then that would already be trouble anyway due to instruction reordering in CPU or memory operation reordering beyond the CPU. (volatile is neither sufficient nor necessary) In a normal emulator, none of that is a problem. Reasonable uses of bitfields are compatible between the x86_64 ELF ABI (Linux, etc.) and the Windows ABI. What you gain is C code that is simultaneously concise and readable. For example, a CPU instruction can be represented in a header file by a union of anonymous structs, each of which contains a padding bitfield and a bitfield for part of the instruction. Access in the C file is then just as easy as for normal struct members. It works for non-CPU hardware too, like DMA descriptors and motherboard registers.
- mytailorisrich 7y agoMacros make code concise and readable. As I said, C's bit fields are often banned in coding guidelines.
- souprock 7y agoHow do you get a macro to make an L-value out of a bitfield? That is, it goes on the left of an assignment. I'm doing stuff like this: ethdev->phy.foo = val; // foo is a bitfield in struct phy cpu->status.irqlevel = newlevel; // irqlevel is a bitfield in the CPU's status word No, I don't want a 2-argument macro (or worse, with an implied variable) called something like SET_FOO or SET_IRQLEVEL. Crummy coding guidelines are not a useful argument against the value of bitfields in the C programming language. Coding guidelines can also ban floating-point types, recursion, or symbol names longer than 6 characters. The problem is not C. The problem is the coding guidelines.
- mytailorisrich 7y agoIt's not because you do not understand something and its reasons to exist (though I hinted at them in previous comments) that it is 'crummy'...