Chinaunix

标题: 请教关于位运算的问题 [打印本页]

作者: wawxdyy    时间: 2011-03-22 14:29
标题: 请教关于位运算的问题
有一个枚举定义如下

  1. enum {
  2.     CASE1    = (1 << 0),
  3.     CASE2    = (1 << 1),
  4.     CASE3    = (1 << 2),
  5.     CASE4    = (1 << 3)
  6. };
复制代码
现在我想定义多于32个得CASE,请问有什么办法,64位位运算怎么做?
作者: rover12421    时间: 2011-03-22 14:35
#define CASE(x) (1<<x)
enum
{
CASE1,
CASE2,
.........
}

CASE(CASE1);
不知道你要干嘛。。。。
作者: wawxdyy    时间: 2011-03-22 14:56
位运算只能允许32位,(1<<32)编译就出错了,不知道有什么办法
作者: sunuslee    时间: 2011-03-22 14:59
顶2L..
enum都可以用2^32 - 1 个CASE了 你还觉得不够..
作者: wawxdyy    时间: 2011-03-22 15:16
回复 4# sunuslee


    我要得位运算。。。
作者: egmkang    时间: 2011-03-22 15:23
枚举是32bit的....
作者: madoldman    时间: 2011-03-22 15:23
回复 5# wawxdyy
  1. 1ULL << 63
复制代码

作者: wawxdyy    时间: 2011-03-22 15:28
回复 7# madoldman


    谢啦
作者: madoldman    时间: 2011-03-22 15:33
回复 6# egmkang


    肯定是和int等长吗?我印象中,标准里面有个地方说枚举的范围是和能跟它兼容的整数类型相同的呢?我翻翻标准去
作者: madoldman    时间: 2011-03-22 15:46
回复 8# wawxdyy

是implementation-defined的呢
Each enumerated type shall be compatible with char, a signed integer type, or an
unsigned integer type. The choice of type is implementation-defined,10 but shall be
capable of representing the values of all the members of the enumeration. The
enumerated type is incomplete until after the } that terminates the list of enumerator
declarations.

10 An implementation may delay the choice of which integer type until all enumeration constants have
been seen.
作者: wb112200    时间: 2011-03-22 15:48
可以用十六进制数 0xf...  一个f 代表4位
作者: egmkang    时间: 2011-03-22 16:04
回复  wawxdyy

是implementation-defined的呢
Each enumerated type shall be compatible with char,  ...
madoldman 发表于 2011-03-22 15:46


我记得我翻过C99的文档,枚举是32bit的......回头我再看看文档
作者: egmkang    时间: 2011-03-23 12:01
回复 10# madoldman

恩,昨天看文档说,枚举和char,integer,unsigned integer兼容.也就是说枚举可以是64bit.....
作者: mirnshi    时间: 2011-03-23 15:17
这还枚举什么呀,直接判断某位不就行了
作者: nizvoo    时间: 2011-03-23 18:49
  1. enum
  2. {
  3. Start0=0x00000000
  4. CASE1,
  5. CASE2,
  6. .....

  7. Start1=0x00010000

  8. CASEN,
  9. CASEM,
  10. .....

  11. Start2=0x00100000

  12. CASEZ,
  13. CASEY,
  14. .....



  15. }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2