I am trying to make a small program where the user inputs numbers between 0 and 31 without limit, and when the input is -1, all the previous values must be printed without repetition. I am trying to use bitwise operators, but all the values between the range are being printed instead of the input values. What should I do?
This is my code:
int main()
{
int x = 0, buffer, z;
while (x != -1) {
printf("User input: \n");
scanf("%d", &x);
buffer = x;
}
for (z = 0; z <= 31;z++) {
if ((buffer & z) == z) {
printf("%d\n", z);
}
}
return 0;
}
I appreciate any help.