简体   繁体   English

我正在阅读开源代码(C++),但不知道为什么他们这样放置枚举

[英]I'm reading open source code (C++) and can't figure out why they put enum this way

I found enum be defined like this and can't figure it out why they put leading zero there.我发现 enum 是这样定义的,无法弄清楚为什么他们把前导零放在那里。

enum SquareDelta {
  DELTA_SSW = -021,
  DELTA_SS = -020,
  DELTA_SSE = -017,
  DELTA_SWW = -012,
  DELTA_SW = -011,
  DELTA_S = -010,
  DELTA_SE = -07,
  DELTA_SEE = -06,
  DELTA_W = -01,
  DELTA_ZERO = 0,
  DELTA_E = 01,
  DELTA_NWW = 06,
  DELTA_NW = 07,
  DELTA_N = 010,
  DELTA_NE = 011,
  DELTA_NEE = 012,
  DELTA_NNW = 017,
  DELTA_NN = 020,
  DELTA_NNE = 021
};

I guess this is not just normal int enum but what is it?我想这不仅仅是普通的 int enum 而是它是什么? could it be in hex like number beginning with "0x"?它可以是以“0x”开头的十六进制数字吗?

Those numbers are octal constants.这些数字是八进制常数。 (Numbers leading with 0 but not 0x are considered in base-8). (以0开头但不是0x的数字在 base-8 中被考虑)。

Thus, -021 == -17, -020 = -16, etc.因此,-021 == -17、-020 = -16 等。

Those are Octal literals and hence they start with 0.这些是八进制文字,因此它们以 0 开头。

What is more interesting & real Question to me is:对我来说更有趣和更真实的问题是:
Why to use Octal Literals as enum values?为什么使用八进制文字作为枚举值?

Probably, because each bit of those octal literals is indicative of something.可能是因为这些八进制文字的每一位都表示某种东西。 It is difficult to make out what, just by seeing the enum and no context where it is being used, but you need to think about it in that direction, and perhaps it will make more sense for you.仅通过查看枚举并且没有使用它的上下文很难弄清楚是什么,但是您需要朝那个方向考虑它,也许它对您来说更有意义。

The other "answers" answer your question, but I'm adding this for informative purposes.其他“答案”回答了你的问题,但我添加这个是为了提供信息。

enum SquareDelta {
               DELTA_NNW= 017,DELTA_NN = 020,DELTA_NNE= 021
DELTA_NWW= 006,DELTA_NW = 007,DELTA_N  = 010,DELTA_NE = 011,DELTA_NEE= 012,
               DELTA_W  =-001,DELTA_ZER= 000,DELTA_E  = 001,
DELTA_SWW=-012,DELTA_SW =-011,DELTA_S  =-010,DELTA_SE =-007,DELTA_SEE=-006,
               DELTA_SSW=-021,DELTA_SS =-020,DELTA_SSE=-017,  
 };

Again, in binary (twos compliment):同样,在二进制(双恭维):

enum SquareDelta {
                 DELTA_NNW=001111,DELTA_NN =010000,DELTA_NNE=010001
DELTA_NWW=000110,DELTA_NW =000111,DELTA_N  =001000,DELTA_NE =001001,DELTA_NEE=001010,
                 DELTA_W  =111111,DELTA_ZER=000000,DELTA_E  =000001,
DELTA_SWW=110110,DELTA_SW =110111,DELTA_S  =111000,DELTA_SE =111001,DELTA_SEE=111010,
                 DELTA_SSW=101111,DELTA_SS =110000,DELTA_SSE=110001,  
 };

So the E/W Coordinate is SquareDelta&7 , and the N/S Coordinate is SquareDelta&070+SquareDelta&4 .所以 E/W 坐标是SquareDelta&7 ,N/S 坐标是SquareDelta&070+SquareDelta&4

Upon further review, it seems that they intended for the least significant octal digit to be on a scale from -2 to 2 to signify the W/E-ness, and the next octal digit to scale from -2 to 2 to signify the N/S-ness.经过进一步审查,他们似乎打算让最低有效八进制数字在 -2 到 2 的范围内表示 W/E-ness,下一个八进制数字从 -2 到 2 表示 N /S-性。 If you add DELTA_W+DELTA_W+DELTA_N and truncate to two octal digits, you get 006, the value of DELTA_NWW .如果添加DELTA_W+DELTA_W+DELTA_N并截断为两个八进制数字,则得到 006,即DELTA_NWW的值。 Since the least significant octal affects the upper, the deltas are limited to plus or minus two.由于最不重要的八进制会影响上部,因此增量限制为正负二。

Those are 8-based ( Edit: the word is octal:) ) numbers.这些是基于 8 的(编辑:这个词是八进制:))数字。

Leading zero makes them base 8 numbers.前导零使它们以 8 为基数。 eg 021 = 17例如 021 = 17

a) Literal constants, beginning with 0 are octal (digits from 0 to 7) a) 以 0 开头的字面常量是八进制(数字从 0 到 7)
b) The actual numbers allow some arithmetics, like N=10 + 2*(E=1) = 12 (NEE). b) 实际数字允许进行一些算术运算,例如 N=10 + 2*(E=1) = 12 (NEE)。

But since NE > N && NW > N, it does not reflect a direction in the circle, so it is, maybe, of limited help, like the word 'SquareDelta' for me.但是由于 NE > N && NW > N,它并不能反映圆中的方向,所以它的帮助可能有限,就像我的“SquareDelta”这个词一样。 Maybe, it makes more sense in context.也许,它在上下文中更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM