Count the 1-bits · นับ 1-bits
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
จำนวน unsigned ทุกตัวถูกจัดเก็บในรูปแบบไบนารี คืนค่าว่าบิตใดมีค่า 1 (สิ่งนี้เรียกว่า population count) ดูที่บิตตำแหน่งต่ำสุดที่มีค่า 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-bits ใน x. ตรวจสอบ bit ต่ำสุดด้วย x & 1u, แล้วเลื่อนขวาด้วย x >>= 1
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่