Count the 1-bits · Compter les bits 1
Count the 1-bits
Every unsigned integer is stored in binary. Return how many of its bits are 1 (this is called the population count). Look at the lowest bit with x & 1u, then shift x right by one with x >>= 1, until x becomes 0.
Compter les bits à 1
Chaque entier non signé est stocké en binaire. Renvoyez le nombre de ses bits qui sont 1 (on appelle cela la population count). Regardez le bit le plus bas avec x & 1u, puis décalez x vers la droite d'un cran avec x >>= 1, jusqu'à ce que x devienne 0.
Complete int popcount(unsigned x) to return the number of 1-bits in x. Check the lowest bit with x & 1u, then shift right with x >>= 1. · Complétez int popcount(unsigned x) pour retourner le nombre de bits 1 dans x. Vérifiez le bit le plus bas avec x & 1u, puis décalez à droite avec x >>= 1.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.