Count the 1-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.
عد البتات 1
يتم تخزين كل عدد صحيح غير الإشارة في النظام الثنائي. أجب عن عدد البتات التي تساوي 1 (يسمى هذا عدّد السكان). انظر إلى أدنى بت يساوي x & 1u، ثم قم بإزاحة x لليمين بمقدار واحد باستخدام x >>= 1، حتى تصبح x مساوية لـ 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. · أكمل int popcount(unsigned x) لإرجاع عدد البتات ذات القيمة 1 في x. تحقق من أدنى بت باستخدام x & 1u، ثم انزلق لليمين باستخدام x >>= 1.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.