about enumeration's range

dengwei1116 2002-12-27 12:05:38
the range of an enumeration holds all the enumeration's enumerator values rounded up to the nearest larger binary power minus 1.the range goes down to 0 if the smallest enumerator is non-negative and to the nearest lesser negative binary power if the smallest enumerator is negative.
以上是Bjarne Stroustrup的The C++ Programming Language第77页关于枚举变量范围的阐述,我不怎么理解这个什么"binary power"?
...全文
45 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengwei1116 2003-01-02
  • 打赏
  • 举报
回复
谢谢你,你的这句话"binary power我认为是指的是以二为底的幂的意思"对我启发很大,我错误地把"binary power"理解成了"二进制数的平方",虽然对e1,e2也是象你那样理解的,但是因为这个错误的思维,总是不能自圆其说,很谢谢你的帖子
俞庆平 2002-12-29
  • 打赏
  • 举报
回复
看了原文,感觉楼主是手动抄写上述内容的,呵呵。
binary power我认为是指的是以二为底的幂的意思。
用我们在数学中学到的内容来看,是以二为底的指数。
不是我的理解和楼主是否一致,但我在推算过程发现我
的想法基本上是合乎Bjarne Stroustrup的说法的。

例如,enum e2 = {a = 3, b = 9};(9d = 1001b)
最近的最小的二次幂应是2^4 = 16, 16 - 1 = 15;
a 为非负值,所以range 0~15;

enum e3 {min = -10, max = 1000000 }; // range -1048576:1048575

最靠近1000000的最小二次幂是2^20 = 1048576;要减一,所以为1048575.
因为min = -10小于零,因为负10有些特殊,所以注意原文的下一句:
This defines the smallest bit-field capable of holding the enumerator values.
所以最小值的边界自然成了 -1048576
(20位)
其实最后一句很说明问题,用最短的位段表示最合适的范围,节约空间啊。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧