Extract Even Bits Only from 32-bit Register

 

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

uint32_t extract_even_bits(uint32_t reg) {
    // Your code here
    uint32_t result = 0; // store the compressed output
    int pos = 0;         // Pos to insert the extracted bit into result

    // Iterate through even bit positions from 0 to 30
    for (int i = 0; i < 32; i += 2) {
        // Create a mask to isolate the current even-positioned bit
        uint32_t mask = 1U << i;
        // Check if the bit at position 'i' is set in 'reg'
        if ((reg & mask) != 0) {
            // If bit is 1, set the corresponding bit in result at position 'pos'
            result |= (1U << pos);
        } 
        // Move to the next bit position in result
        pos++;
    }

    return result;
}


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

 

 

 

 

Loading...

Input

85

Expected Output

15