DIP: add Bit Fields

Walter Bright newshound2 at digitalmars.com
Fri Mar 8 19:52:37 UTC 2024


To clarify a bit, C allows ints to be 18 bits (e.g. for a PDP-10). But D defines 
them as 32 bits. Technically, we're crazy to do that. But practically, we can 
rely on the people who implement C compilers to be sane, and CPU makers have 
long accepted the idea that ints are 32 bits. We are on very safe ground there.

Even chars being signed by default, although popular in the 80s, has disappeared 
from modern compilers.

We simply do not have to worry about the C bitfield layout being implementation 
defined. No sane person is going to invent another scheme for a C compiler if at 
all possible.

There are two dominant schemes today, VC and gcc. I'm not aware of any other 
scheme for a modern C compiler (Digital Mars C emulates the VC layout). Even so, 
VC and gcc lay things out the same way for `int` bit fields. This is never going 
to change.

Just stick with int bit fields and some common sense, and you're code will be fine.

Much of D's design (and many other languages' design) is predicted on C behavior 
not being implementation defined, even though it technically is.

There's no need to solve a non-existent problem.

(Yes, I know, there is a version of D that has 16 bit ints. But that's a special 
build of the compiler, and it is technically not a D compiler :-/ and the people 
who use it understand that and it isn't a big problem for them.)


More information about the dip.development mailing list