Little Endian to Big Endian

Code

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

uint32_t convert_endian(uint32_t value) {
    // Write logic to swap bytes
    // uint8_t mask = 0;
    // uint8_t temp = 0;

    // mask = value & 0xff;

    // value = value >> 8;

    // value |= (mask << 24);

    // mask = value & 0xff;

    // temp = (value >> 16) & 0xff;

    // value = value & ~(0xff << 16);
    // value |= mask << 16;

    // value = value & ~(0xff);
    // value |= temp;

    uint32_t byte0 = (value & 0xff) << 24;
    uint32_t byte1 = (value & 0xff00) << 8;
    uint32_t byte2 = (value & 0xff0000) >> 8;
    uint32_t byte3 = (value & 0xff000000) >> 24;

    return byte0|byte1|byte2|byte3;
}

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

Solving Approach


 

Upvote
Downvote
Loading...

Input

305419896

Expected Output

2018915346