Little Endian to Big Endian

Code

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

uint32_t convert_endian(uint32_t value) {
    // Write logic to swap bytes

    value = (value & 0x00FF00FF) <<  8 | (value & 0xFF00FF00) >> 8;
    value = (value & 0x0000FFFF) << 16 | (value & 0xFFFF0000) >> 16;

    return value;
}

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

Solving Approach


First, we swap the even and odd (2 bytes order) and then swap the last 4 bytes and the first 4 bytes.

 

Upvote
Downvote
Loading...

Input

305419896

Expected Output

2018915346