operator << used in for loop and if statement

hello, i came across some c++ codes using following statements :

for(int i=0;i<(1<<n);i++){
some statements
..............}

and

if(n&(1<<i)){
statement }

and

int dp[1<<21];

now i dont understand what 1<<n means...i have never used << operator like this before and was not able to find anything about it on the net
It is bitwise shift operator. It shifs the value of the left operand the number of times specified by the right operand.
1 << n <=> 2^n, where ^ means power of.
Since at least 20 years modern compilers will internally translate 2^n to this bitshift if supported by the CPU (nearly every CPU does support it).

int dp[1<<21]; is interesting because it addresses a really big array with at least sizeof(int) * (1 + 2^21) bytes.
1 << n <=> 2^n, where ^ means power of.

It should be noted, however that the caret does not mean power of in C++, so

Since at least 20 years modern compilers will internally translate 2^n to this bitshift
would be wrong. As 2^n has a decidedly different meaning than 2 to the power of n in C++.

int dp[1<<21]; is interesting because it addresses a really big array with at least sizeof(int) * (1 + 2^21) bytes.

Well, it defines one with exactly sizeof(int*)*(1<<21) bytes, anyway.
thankyou all...it helped a lot
@cire: Ok, you're right with the meaning of ^ in C++. But I've defined it to the power of. So in this context its quite right the power of. Your other statements I agree with you.
Topic archived. No new replies allowed.