简体   繁体   中英

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 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"?

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

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

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.

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.

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

Leading zero makes them base 8 numbers. eg 021 = 17

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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