jeudi 30 juillet 2015

Why in for loop this conditions multilpy and divide by two (when i use >>= in for loop)

I was practicing some sample in c/c++ i found a code where the code is given below :

unsigned int num;
int c = 0;
cin >> num;
for (; num; z=num <<= 1)
{
    if (int y=num & 1)
    {
        c++;
    }
}
cout << "c : " << c << endl;

So i have following questions in mind:

(1) In this for loop condition the second parameter is num (what is the significance keeping it num, why not any condition like num<29 or may be some other value).

(2) Whereas the third parameter is z=num <<= 1 , here i had kept z to do some debugging to know the value obtained on this condition i found that it just doubles when symbol is >>= and halves when <<= , Why this behavior ?

(3) In the if condition we have if (int y=num & 1) i am not able to understand what it do by & 1?

I tried to understand it by debugging but i want to know clear explanation of this three questions so thats why wanted to ask experts.

Aucun commentaire:

Enregistrer un commentaire