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

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.

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 1 << n <=> 2^n, where ^ means power of. |

It should be noted, however that the caret does not mean

Since at least 20 years modern compilers will internally translate 2^n to this bitshift |

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

Well, it defines one with exactly

Topic archived. No new replies allowed.