Count the 1-bits · 数 1 的位
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
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 的位
每个无符号整数都以二进制存储。返回其中有多少位是 1(这叫做位计数,population count)。用 x & 1u 查看最低位,再用 x >>= 1 把 x 右移一位,直到 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),返回 x 中值为 1 的位的个数。用 x & 1u 检查最低位,再用 x >>= 1 右移。
Click Run to see the output here. · 点击“运行”查看此处输出。