0

I was reading this code

there is a line : pair <int, int> approach[1 << 18][17]

I don't know what is the meaning of this declaration : approach[ 1<<18 ][17];

Can anyone help?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kadaj13
  • 1,423
  • 3
  • 17
  • 41

3 Answers3

3

In this context, << is the bit left shift operator. 1 << 18 means take the binary representation of 1 and shift it to the left by 18. This is 218 (2 to the power 18, or 262144). So you have a very large 2D array of pairs:

pair <int, int> approach[262144][17];
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
2

<< is the bit left shift operator.

So 1 << 18 is an integer constant that has a value of 218.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

It simply means 2^18, 2 to the power of 18.

The code is missing some explanations, the only real good informations is

// SGU 502 -- Digits Permutation

Ah its about digit permutation, so

pair <int, int> approach[1 << 18][17]

might be used to store the permutations, unless there is some limitation on the permutations the amount of permutations should be N! (hopefully N! <= (1<<18)).

But the definition doesn't tell anything about that, lets see if we can make it more clear (and hopefully right).

const int maxLength = 17;
const int maxPermutation = 1 << (maxLength+1);
pair <int, int> approach[maxPermutation ][maxLength]
static_assert(factorial(maxLength) <= maxPermutation, "approach might not be able to hold all permutations");
Surt
  • 15,501
  • 3
  • 23
  • 39