a-z
a-z

Reputation: 1664

typedef array of constant length

What does this do and how?

typedef int map_t [1<<MAX];

What does that line do?

Upvotes: 0

Views: 2036

Answers (2)

Esailija
Esailija

Reputation: 140210

Let's assume MAX is defined as 8:

typedef int map_t[1<<MAX];

is same as

typedef int map_t[256];

because 1 << 8 = 256. It means shift bits in the number 1 8 times to the left like so:

1 is 0000 0001 in binary

after 1 shift:

0000 0010, which is 2 in decimal

after 8 shifts:

1 0000 0000, which is 256 in decimal

So there is no bitmasking here, it's just easy way to say

typedef int map_t[pow(2,MAX)];

I haven't done C much but the above should compile, right?

Edit: The above doesn't compile but if we were to dynamically allocate it should work.

Upvotes: 6

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

Assuming MAX is a constant known at compile-time, then this code:

typedef int map_t [1<<MAX];
map_t x;

is the same as this code:

int x[1 << MAX];

Upvotes: 8

Related Questions