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) 
{
    int i;
    int j=0;
    uint32_t ans=0x00000000; //32 bit representation of 0.
    for(i=0;i<32;i=i+2) //traversing through all bits but incrementing by 2 as we need only even positions.
    {
        if(reg&(1<<i)) //checking if respective ith bit is set or not.
        {
            ans=ans|(1<<j); //we need to have seperate variable to track next position to set in ans, hence we took seperate variable.
            j++;
        }
    }
    return ans;
}

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

Solving Approach

 

 

 

Loading...

Input

85

Expected Output

15