我正在阅读开源代码(C++)并且无法弄清楚为什么他们这样放置枚举
我发现枚举是这样定义的,但无法弄清楚为什么他们在那里放前导零。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这些数字是八进制常数。 (以
0
开头但不以0x
开头的数字被视为以 8 为基数)。因此,-021 == -17,-020 = -16,等等。
Those numbers are octal constants. (Numbers leading with
0
but not0x
are considered in base-8).Thus, -021 == -17, -020 = -16, etc.
这些是八进制文字,因此它们以 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.
其他“答案”回答了您的问题,但我添加此内容是为了提供信息。
同样,在二进制(二进制补码)中:
因此,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.
Again, in binary (twos compliment):
So the E/W Coordinate is
SquareDelta&7
, and the N/S Coordinate isSquareDelta&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 ofDELTA_NWW
. Since the least significant octal affects the upper, the deltas are limited to plus or minus two.这些是基于 8 的数字(编辑:这个词是八进制:))。
Those are 8-based (Edit: the word is octal :) ) numbers.
前导零使它们以 8 为基数。例如 021 = 17
Leading zero makes them base 8 numbers. e.g. 021 = 17
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.