This is supposed to count the number of 1s in the binary representation of an integer. I know there are some logical errors.Code:void printOnes (int x) { int i; int numOnes = 0; for (0 = i; i < 32; i++) { if ((x & 0x1) = 1) numOnes++; x = x >> 1; } printf("The number of ones is %c\n", numOnes); }
I know the logical operator x & 0x1 simply seeing if it's odd but I don't see how this counts the number of 1s. (isn't is just saying x % 2 == 1. so x could be 3,5..etc) How would I fix this?
or is x=x>>1 wrong?