Skip to content · ⁨ข้ามไปยังเนื้อหา⁩

Count the 1-bits · ⁨นับ 1-bits⁩

English

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

Log in or create account · ⁨เข้าสู่ระบบหรือสร้างบัญชี⁩

IGCSE, A-Level & AP