I was practicing some sample in C++ I found a program where the code is given below:
unsigned int num;
int c = 0;
cin >> num;
for (; num; num <<= 1)
{
if (num & 1)
{
c++;
}
}
cout << "c : " << c << endl;
So I have following questions in mind:
In this for loop condition, the second parameter is
num
— what is the significance keeping it asnum
; why not any condition likenum<29
or may be some other value?Whereas the third parameter is
z=num <<= 1
, here I had keptz
to do some debugging to know the value obtained on this condition, but I found that it just doubles when the operator is>>=
and halves when it is<<=
. Why this behavior?In the if condition we have
if (int y=num & 1)
. I am not able to understand what it does with& 1
?
I tried to understand it by debugging but I want to know clear explanation of these three questions so that's why I wanted to ask experts.