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 new_reg = 0;
    for(int i=0;i<32;i++){
        if(!(i%2)){
            new_reg |= ((reg >> i) & 0x1) << i/2;
        }
    }

    return new_reg;
}

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

Solving Approach

Can be optimized more by iterating over only the even bits in the for loop

 

 

Upvote
Downvote
Loading...

Input

85

Expected Output

15