abraxas334

October 11th, 2010, 03:36 PM

Just a quick question about some code i am trying to understand.

Looks something like this:

...

int myRand;

myRand = rand(); //creates rand int between 0 and RANDMAX

int shift = 1<<1;

if(myRand & shift)

{

std::cout<<"True \n";

}

else

{

std::cout<<"False ";

}

...

so the shift variable is obviously 2. Which in binary is 10.

Why is 1717471323 & 2 true. and 833761856 & 2 false?

I just don't understand what the if statement tries to do. What makes it true and what makes it fail?

I understand that say 60 & 3 is equal to 12 but how could it result in a true or false?

Any help would be greatly appreciated.

Looks something like this:

...

int myRand;

myRand = rand(); //creates rand int between 0 and RANDMAX

int shift = 1<<1;

if(myRand & shift)

{

std::cout<<"True \n";

}

else

{

std::cout<<"False ";

}

...

so the shift variable is obviously 2. Which in binary is 10.

Why is 1717471323 & 2 true. and 833761856 & 2 false?

I just don't understand what the if statement tries to do. What makes it true and what makes it fail?

I understand that say 60 & 3 is equal to 12 but how could it result in a true or false?

Any help would be greatly appreciated.