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 val_pos = 0;
    uint32_t val=0;
    uint32_t bit_val = 0;
    uint32_t target_pos = 0;
    for(uint32_t i=0;i<32;i+=2){
        //for loop starts for 32 bit input
        bit_val = (reg>>i) & 1U;
        //i goes from the LSB of input and checks for 1 then puts in bit_val
        val |= (bit_val<< target_pos);
        //bit_value slides across on output value according target_pos to make output
        //consecutive
        target_pos++;
        //target position tick increased for next 
    
    }
    return val;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

85

Expected Output

15