- 1
How to calculate the sum of binary code ?
Example: the binary code of number 9 is 1001. I want to calculate and get the result 2. 1+0+0+1 = 2. What should I do ?
8 Réponses
+ 3
You can simply use the built-in function __builtin_popcount().
It returns number of set bits in binary representation of a number and that's what the sum of all 1's is.
+ 2
Thanks Martin
+ 1
Kiran Deep Naidu Thanks for your help.
0
I hope this helps you😊😊
0
Taylor, I have corrected the code as you said but I still wonder this gonna not be appropriate one because we need to apply the 2's complement for finding the binary of negative number.