22. Extract Even Bits Only from 32-bit Register

Back To All Submissions
Previous Submission
Next Submission
#include <stdio.h>
#include <stdint.h>

uint32_t extract_even_bits(uint32_t reg) {
    uint16_t op;
    for(int i=0;i<16;i++){
        int bit = (reg >> i*2) & 1; // extract the bit
        op |= (bit<<i); //set the bit at the req position
    }
    return op;
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote