我正在阅读开源代码(C++)并且无法弄清楚为什么他们这样放置枚举

发布于 2024-11-29 20:02:45 字数 487 浏览 4 评论 0原文

我发现枚举是这样定义的,但无法弄清楚为什么他们在那里放前导零。

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
};

我想这不仅仅是普通的 int 枚举,但它是什么?它可以是以“0x”开头的十六进制数字吗?

I found enum be defined like this and can't figure it out why they put leading zero there.

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? could it be in hex like number beginning with "0x" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我只土不豪 2024-12-06 20:02:45

这些数字是八进制常数。 (以 0 开头但不以 0x 开头的数字被视为以 8 为基数)。

因此,-021 == -17,-020 = -16,等等。

Those numbers are octal constants. (Numbers leading with 0 but not 0x are considered in base-8).

Thus, -021 == -17, -020 = -16, etc.

兮子 2024-12-06 20:02:45

这些是八进制文字,因此它们以 0 开头。

更有趣的是&对我来说真正的问题是:
为什么使用八进制文字作为枚举值?

可能是因为这些八进制文字的每一位都表示某些东西。仅通过查看枚举并且没有使用它的上下文很难弄清楚它是什么,但您需要朝那个方向思考它,也许它对您来说更有意义。

Those are Octal literals and hence they start with 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.

谎言月老 2024-12-06 20:02:45

其他“答案”回答了您的问题,但我添加此内容是为了提供信息。

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,  
 };

同样,在二进制(二进制补码)中:

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,  
 };

因此,E/W 坐标为 SquareDelta&7,N/S 坐标为 SquareDelta&070+SquareDelta&4

经过进一步审查,他们似乎打算将最低有效八进制数字设置为从 -2 到 2 的范围以表示 W/E 性,并将下一个八进制数字设置为从 -2 到 2 范围以表示 N /S-性。如果添加 DELTA_W+DELTA_W+DELTA_N 并截断为两个八进制数字,则会得到 006,即 DELTA_NWW 的值。由于最不重要的八进制会影响上位八进制,因此增量被限制为正负二。

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.

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. If you add DELTA_W+DELTA_W+DELTA_N and truncate to two octal digits, you get 006, the value of DELTA_NWW. Since the least significant octal affects the upper, the deltas are limited to plus or minus two.

绿光 2024-12-06 20:02:45

这些是基于 8 的数字(编辑:这个词是八进制:))。

Those are 8-based (Edit: the word is octal :) ) numbers.

挽容 2024-12-06 20:02:45

前导零使它们以 8 为基数。例如 021 = 17

Leading zero makes them base 8 numbers. e.g. 021 = 17

烟火散人牵绊 2024-12-06 20:02:45

a) 文字常量,从 0 开始是八进制(从 0 到 7 的数字)
b) 实际数字允许进行一些算术运算,例如 N=10 + 2*(E=1) = 12 (NEE)。

但由于 NE > N&&西北> N,它不反映圆圈中的方向,所以它的帮助可能有限,就像“SquareDelta”这个词对我来说。也许,结合上下文更有意义。

a) Literal constants, beginning with 0 are octal (digits from 0 to 7)
b) The actual numbers allow some arithmetics, like 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. Maybe, it makes more sense in context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文