devLupin / algorithm

PS
1 stars 0 forks source link

10진수 -> 2진수 1의 개수 세는 방법(bitset) #9

Open devLupin opened 1 year ago

devLupin commented 1 year ago
#include <bitset>
bitset<5> bit(9);
cout << bit.to_string();    // 01001
cout << bit.count();    // 2
int count(int n) {
    int i;
    for (i = 0; n != 0; i++) {
        n &= (n - 1);
    }
    return i;
}
devLupin commented 1 year ago