114. Little Endian to Big Endian

Back To All Submissions
Previous Submission
Next Submission

Code

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

uint32_t convert_endian(uint32_t value) {
    // Write logic to swap bytes
    uint32_t result = 0;

    //1. Extract first byte
    result |= (value & 0xFF) << 24;

    //2. Extract second byte
    result |= (value & 0xFF00) << 8;

    //3. Extract third byte
    result |= (value & 0xFF0000) >> 8;

    //4. Extract fourth byte
    result |= (value & 0xFF000000) >> 24;

    return result;
}

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

Solving Approach


 

Was this helpful?
Upvote
Downvote