All submissions

Count Set Bits in an 8-bit Register

Code

#include <stdio.h>
#include <stdint.h>

uint8_t count_set_bits(uint8_t reg) {
    // Your code here
int Count = 0;
    for(int i=7;i>=0;i--){
        if(reg>>i&1)
            Count++;
    }
    return Count;
}

int main() {
    uint8_t reg;
    scanf("%hhu", &reg);
    printf("%u", count_set_bits(reg));
    return 0;
}

Solving Approach

 

 Code

#include <stdio.h>
#include <stdint.h>

uint8_t count_set_bits(uint8_t reg) {
    // Your code here
int Count = 0;
    for(int i=7;i>=0;i--){
        if(reg>>i&1)
            Count++;
    }
    return Count;
}

int main() {
    uint8_t reg;
    scanf("%hhu", &reg);
    printf("%u", count_set_bits(reg));
    return 0;
}

Solving Approach

 

Step 1:- initialize the count variable with zero

int Count = 0;

Step 2:- iterate the loop up to 7

    for(int i=7;i>=0;i--){

Step 3:- checking the bit is set or not if bit is set condition will true then increment count.

        if(reg>>i&1)

            Count++;

    }

Step 4:- return the count variable.

    return Count;

}

 

 

Loading...

Input

0

Expected Output

0