All submissions

Extract Even Bits Only from 32-bit Register

Code

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

uint32_t extract_even_bits(uint32_t reg) {

    uint32_t sol=0x00000000;

    uint32_t bit_counter=0;

    for(uint32_t i=0; i<32; i++)
    {
        // check if the pos is even
        if(i%2 ==0 || i==0)
        {
            // store the pos value

            // if the bit is 1
            if(reg & (1<<i))
            {
                // push back 1
                sol |= (1<<bit_counter);
            }

            // else if the bit is 0
            else
            {
                // push back 0
                sol &= ~(1<<bit_counter);
            }

            // increase bit ounter by 1
            bit_counter++;
        }
    }
    return sol;
}

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

Solving Approach

  • Check if the bit location is even

  • if the even pos bit is 1 push-back 1, else pus- back 0

 

Loading...

Input

85

Expected Output

15