4 ms·
I believe the point is that C bitfields allow sub-word addressing; the variable refers to specific bits within a word. The Rust flags is the same as using bitwi
by mcguire 9y ago
I believe the point is that C bitfields allow sub-word addressing; the variable refers to specific bits within a word. The Rust flags is the same as using bitwise operations on word-sized values.
To be honest, I don't know if a functional difference, but I have not done any embedded programming where there might be a difference.
- eridius 9y agoAccessing bitfields in C just ends up compiling to bitwise operations on word-sized values anyway.
- AnimalMuppet 9y agoI've seen plenty of hardware registers that use one bit for some X, two bits for some Y, and 5 bits for some Z. (Yes, in embedded programming.) One way of dealing with that is with C bitfields. Now, I never did it that way myself. I just and-ed it with a bitmask to get the part that I wanted. But it's a bit messier to do it that way, because if you want one of the fields as a number, and it's not from the least-significant bits, then you have to shift it as well as mask it, whereas with bitfields you can just read it as a number and get the right value.
- pjmlp 9y agoSpend a few minutes writing wrapper functions and its done. Never bothered me on Turbo Pascal days, plus if I remember correctly bitfield ordering is not portable across compilers anyway.