All submissions

Little Endian to Big Endian

Code

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

uint32_t convert_endian(uint32_t value) 
{
    uint32_t ans=0x00000000;
    unsigned char *ptr=(unsigned char *)(&value); 

    for(int i=0;i<4;i++)
    {
        ans=ans|((*ptr)<<(8*(3-i))); //through this logic we are making sure that first extracted byte from input will be placed in the last byte. 
        //For that it needs to shiftedby 24 times.SImilarly for next byte it need to shift 16 times.
        ptr++;
    }

    return ans;
}

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

Solving Approach


 

Loading...

Input

305419896

Expected Output

2018915346