Topic: enum and underlying types
Author: Jack Klein <jackklein@att.net>
Date: Tue, 5 Sep 2000 17:37:43 GMT Raw View
On Sun, 3 Sep 2000 12:30:15 GMT, assert@my-deja.com wrote in
comp.std.c++:
> I want to explore what the lower bound for bits is
> for an underlying type to represent an enumeration.
>=20
> enum E { a =3D 8, b =3D 15 };
>=20
> Can an integral type with 3 bits of representation
> be the underlying type if E ?
No.
> Or is it at least 4 ?
Yes.
> 7.2 p5 suggests that the underlying type will at
> least have enough bit patterns as there are enum values.
This can be confusing if taken in isolation.
> 7.2 p6
>=20
> For an enumeration where e_min is the smallest enumerator and e_max i=
s
> the largest, the values of the enumeration are the values of the
> underlying type in the range b_min to b_max , where b_min and
> b_max are, respectively, the smallest and largest values of
> the smallest bit=ADfield that can store e_min and e_max.
Did you note footnote 81, on the same page as the paragraph above,
which describes what the terms b_min and b_max mean?
=3D=3D=3D=3D=3D=3D=3D=3D
81) On a two?s=ADcomplement machine, bmax is the smallest value greater
than or equal to max (abs(emin ) - 1 , abs(emax ) ) of the form 2^M
- 1; bmin is zero if emin is non=ADnegative and - (bmax + 1 ) otherwise.
> That last part is hard for me to get, what exactly is
>=20
> "the smallest bit field that can store e_min and e_max" ?
The question is not relevant. The proper question is:
"what is the smallest bit field that can store b_min and b_max"?
If the minimum value of the enum is greater than 0, the bit field must
be large enough to store all values from 0 through e_emax, inclusive.
> does that mean if e_min is 8 and e_max is 15, that a
> 3 bit bit-field can store 8 to 15 since there is a mapping
> of the values 8 to 15 into the 3 bit bit-field ?
>=20
> 8 -> 000
> 9 -> 001
> ...
> 15 -> 111
No, this is not legal. The bit field must be able to contain the
value 0 even if 0 is not one of the enumeration constants. bmin is
never greater than 0.
> Or does that mean that all values from zero up
> to the value of e_max can must represented in the bitfield ?
Yes, and possibly more if any of the enumerated values are negative.
> If this is what it means then why not just say
>=20
> "the smallest bit-field that can store values from zero to e_max"
>=20
> Why even mention e_min ?
I believe Yaroslav already explained that adequately. Consider an
enum with all negative values, for example -6 to -1.
> I tend to think that the first interpretation is correct
> (and that E may be represented with a 3 bit integral type)
> but I have seen posts regarding enum types that suggest
> the second interpretation, that is why I ask for clarification.
>=20
> As a final note I know that 3 bit integral types are hard
> to come by :) but I wanted my example to focus on the rules
> regarding enum and not distract from the issue by choosing a lot
> of non-obvious enum values that might express the
> same problem where the bit boundry is a 33 or 34 bit
> integral type.
>=20
> Thanks.
The issue about bit field sizes smaller than CHAR_BIT is not
far-fetched at all. If you are defining a bit-field type containing
the enumeration value, it should indeed occupy no more than the
minimum number of bits specified by the standard.
Jack Klein
--=20
Home: http://jackklein.home.att.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: assert@my-deja.com
Date: Sun, 3 Sep 2000 12:30:15 GMT Raw View
I want to explore what the lower bound for bits is
for an underlying type to represent an enumeration.
enum E { a = 8, b = 15 };
Can an integral type with 3 bits of representation
be the underlying type if E ?
Or is it at least 4 ?
7.2 p5 suggests that the underlying type will at
least have enough bit patterns as there are enum values.
7.2 p6
For an enumeration where e_min is the smallest enumerator and e_max is
the largest, the values of the enumeration are the values of the
underlying type in the range b_min to b_max , where b_min and
b_max are, respectively, the smallest and largest values of
the smallest bit field that can store e_min and e_max.
That last part is hard for me to get, what exactly is
"the smallest bit field that can store e_min and e_max" ?
does that mean if e_min is 8 and e_max is 15, that a
3 bit bit-field can store 8 to 15 since there is a mapping
of the values 8 to 15 into the 3 bit bit-field ?
8 -> 000
9 -> 001
...
15 -> 111
Or does that mean that all values from zero up
to the value of e_max can must represented in the bitfield ?
If this is what it means then why not just say
"the smallest bit-field that can store values from zero to e_max"
Why even mention e_min ?
I tend to think that the first interpretation is correct
(and that E may be represented with a 3 bit integral type)
but I have seen posts regarding enum types that suggest
the second interpretation, that is why I ask for clarification.
As a final note I know that 3 bit integral types are hard
to come by :) but I wanted my example to focus on the rules
regarding enum and not distract from the issue by choosing a lot
of non-obvious enum values that might express the
same problem where the bit boundry is a 33 or 34 bit
integral type.
Thanks.
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]