Extract Even Bits Only from 32-bit Register

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

#define REG_SIZE 32
#define FIRST_BIT_POS 1

uint32_t extract_even_bits(uint32_t reg) {
    // Your code here
    uint32_t output = 0;

    for (uint8_t i = 0; i < REG_SIZE / 2; i++)
    {
        uint8_t mask = (reg >> (i * 2)) & FIRST_BIT_POS;
        output |= (mask << i);
    }

    return output;
}

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