Article View: comp.std.c
Article #4862Re: Bit-field sizes
From: Larry Jones
Date: Wed, 06 Mar 1996 17:01
Date: Wed, 06 Mar 1996 17:01
55 lines
2761 bytes
2761 bytes
In article <nzRPxQ9ytZZA084yn@csn.net>, thads@csn.net (Thad Smith) writes: > Can the size of a bit-field, measured in bits, exceed the size of an > int? The description of bit-fields describes it as an integral > type with a (programmer-)specified number of bits, not specifically > constrained in size. ANSI Classic section 3.2.1.1 says, however, that > bit-fields are promoted to either int or unsigned int. This would > seem to prohibit promoting a large bit-field to a long. If so, why > was this constraint imposed? No. ANSI Classic 3.5.2.1: The expression that specifies the width of a bit-field shall be an integral constant expression that nas nonnegative value that shall not exceed the number of bits in an ordinary object of compatible type. The constraint is historical, probably because extracting a bit-field that is longer than a word is sufficiently complicated as to be contrary to the ``spirit of C''. > Secondly, is the storage unit within which bit-fields are allocated > constrained to be a single size or can it vary, depending on the size > of the bit fields? I see nothing in the standard prohibiting > different size storage units for bit-fields. Also bit-fields are > explicitly allowed to span multiple storage units, at least if the > bit-field is not the first in a structure (the use of the word "may" > makes the requirement for the first bit-field ambiguous with regard to > storage in a single unit). It can vary (my emphasis): An implementation may allocate *any* addressable storage unit large enough to hold a bit-field. Thus, an implementation is free to allocate a char for a 1 bit field and an int for a 14 bit field. > If different size storage units are allowed or any bit-field is > allowed to span multiple units, the size of a struct containing > bit-fields might be 1 byte, while bit-fields could be allocated up to > the size (in bits) of ints, possibly four bytes in a particular > implementation, by choosing larger storage units for larger > bit-fields. Correct. > Is anyone aware of an implementation using bit-field storage units > smaller than int? Lots of them. ---- Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH 45150-2789 513-576-2070 larry.jones@sdrc.com In my opinion, we don't devote nearly enough scientific research to finding a cure for jerks. -- Calvin
Technical Details
Message-ID:
<4hkgds$bbh@info1.sdrc.com>Path:
arc1-ca.pugleaf.net!sdrc.com!thor!scjonesReferences:
<nzRPxQ9ytZZA084yn@csn.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sdrc.com!thor!scjones From: larry.jones@sdrc.com (Larry Jones) Newsgroups: comp.std.c Subject: Re: Bit-field sizes Date: 6 Mar 1996 17:01:48 GMT Organization: SDRC Engineering Services Distribution: world Message-ID: <4hkgds$bbh@info1.sdrc.com> References: <nzRPxQ9ytZZA084yn@csn.net> NNTP-Posting-Host: thor.sdrc.com Originator: scjones@thor