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?
intis a 32 bit type. You initializenumto0x00008000, and all bits are significant.(unsigned) short int, or better,(u)int16_t, then you will get the result you want.