TypechoJoeTheme

IT技术分享

统计

Count Number of 1 in a Binary Integer

2017-06-16
/
0 评论
/
913 阅读
/
正在检测是否收录...
06/16

1. Description

Calculates the number of 1 in a binary integer

2. Example

11111110100 -> 8
001011010 -> 4

3. Code

int countBit(int x) {
    int count = 0;
    while(x != 0) {
        count++;
        x &= (x - 1);
    }
    return count;
}
Digital
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/376/(转载时请注明本文出处及文章链接)