dimanche 29 août 2021

Convert Little Endian to Big Endian - Not getting expected result

I have a very small code where I am trying to convert a 16 bit number from little endian to big endian format.

The value of number is 0x8000 and after conversion I am expecting it to be as 0x0080 - but I am getting some different value as mentioned below:

#include <iostream>
int main()
{
        int num = 0x8000;
        int swap = (num>>8) | (num<<8);
        std::cout << swap << std::endl;
        return 0;
}

The program outputs 8388736 as value which in hex is 0x800080 - I am not sure what wrong am I doing?

Aucun commentaire:

Enregistrer un commentaire