Hello beautiful people, I need a help with output of this program:
#include <stdio.h>
int main() {
int x,y;
scanf("%d %d",&x,&y);
int t = x^y;
int p = 0;
while (t > 0) {
p += t%2;
t /= 2;
}
printf("%d", p);
return 0;
}
I tried to write it down on paper and do some work by hand. So i wrote this :
lets say for x = 2 and y = 4
first iteration:
p = 0 + 16mod2 which is equal to 0
t = 8
second iteration:
p = 0 + 8mod2 which is equal to 0
t = 4
third iteration:
p = 0 + 4mod2 which is equal to 0
t = 2
forth iteration:
p = 0 + 2mod2 which is equal to 0
t = 1
And output should be 0, but somehow when I run code I get 2. Can someone help me out with this one please? And are there any other cases to consider, like what if x = 0, y = 0 or x and y are < 0 ?