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) {
    // Your code here
    int result=0;
    int pos=0;
    uint32_t x= reg&0x55555555;
    for(int i=0;i<32;i+=2){
        if(x&(1<<i)){
            result|=(1<<pos);
        }
        pos++;
    }
     
    return result;
}

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

Solving Approach

Here, use a bit mask to keep only even bits and do an AND operation to clear all odd bits. Iterate through the loop to find whether the even bit(i th) is set. Store it in the result variable and increment the pos variable

 

 

Loading...

Input

85

Expected Output

15