Need an Explanation for the code.

I wrote a code like this

#include<iostream>
using namespace std;
int main()
{ 
int w=32;
int x=1<<w;
int z=1<<32;
cout<<"X : "<<x<<endl; //Case 1
cout<<"Z : "<<z<<endl; //Case 2
return 0;
}

On running output is X : 1 and Z : 0
Please help me how answer is different in the two cases.

First of all X and Z both are equal to zero . Next thing , that as data type ‘int’ is 32 bit and ,1 when shifted left 32 bits leaves all zeroes , so you get 0 as answer.
Try 2>>4
you

First of all X and Z both are equal to zero . Next thing , that as data type ‘int’ is 32 bit and ,1 when shifted left 32 bits leaves all zeroes , so you get 0 as answer.
Try 2>>4
you’ll get 32
and 2>>32 gives zero too
whereas 2>>30 gives –2,147,483,648

both are 0s on ideone - http://ideone.com/9O7zU9

1 Like

thanks…