How to compute set bits in negative number

how to compute set bits in negative number

You can use __builtin_popcount(i) function to get set bits in a number. But negative numbers are stored in their 2’s complement so you will get a lot of set bits in a negative number.
You can check out for -2.

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.