I posted this two days ago, but since my question has since changed to something else and I now need help regarding a new problem in my code, I thought it would be alright to make a new post. If I'm wrong, please close this question; I'm new to the community and don't want any trouble. I just want some help with my problem.
I've received some feedback: I know my two problems with this code: I need to be checking that the input by the user is within range (0~2^32 - 1; if it isn't in range, it gets the "warning" message), and I need to check for negative numbers (these should get the "invalid" warning). But I'm completely stumped. Could I please perhaps get some more help on working my code to do the above two things?
Update: I think this code finally did the trick. Please let me know if I'm overlooking an error that isn't showing up after being compiled. Thanks!
#include <stdio.h>
int main() {
long long int num;
int count = 0;
printf("Please enter an integer: ");
scanf("%lld", &num);
if (num >= 0 && num <= 4294967295) {
while(num){
count += num & 1;
num >>= 1;
}
printf("%d\n", count);
} else {
printf("Warning: this is not a valid integer\n");
}
return 0;
}