Count Set Bits in an 8-bit Register

Code

#include <iostream>
using namespace std;

int countSetBits(unsigned int reg) {
    int count = 0;

    for(int i = 0; i < 8; i++) {
        if(reg & (1 << i))
            count++;
    }

    return count;
}

int main() {
    unsigned int reg;
    cin >> reg;

    cout << countSetBits(reg);

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Expected Output

0