All submissions

Extract Even Bits Only from 32-bit Register

Code

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

uint32_t extract_even_bits(uint32_t reg) {
    // Your code here
    uint32_t mask = 0; // mask = 0000 0000 0000 0000 0000 0000 0000 0000
    int i, count = 0;
    for ( i = 0; i < 32; i++ )
    {
        if ( ( i % 2 == 0 ) && ( reg & ( 1 << i ) ) ) // if the index position is even and if the given register value
            // has bit set to 1 to that even index position of the given reg value
        {
              count++; // then increase count by 1
        }
    }
    for ( i = 0; i < count; i++ )
    {
        mask = mask | ( 1 << i ); // 
    }
    return mask;
}

int main() {
    uint32_t reg;
    scanf("%u", &reg);
    printf("%u", extract_even_bits(reg));
    return 0;
}

Solving Approach

may not the be the efficient solution but it works. Please read the comments

 

 

 

Loading...

Input

85

Expected Output

15