how to count set bits of a negative decimal no…
Set bits of a negative decimal no
@vsinghal277 Cast number to unsigned int and perform your counting on that:
int numOnesInBinary(int number) {
int numOnes = 0;
unsigned int unumber = static_cast(number);
while (unumber != 0) {
if ((unumber & 1) == 1) {
numOnes++;
}
unumber >>= 1;
}
return numOnes;
}
Hope you get it.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.