#include <iostream>
#include <cstdlib>
using namespace std;
typedef unsigned char uchar_t;
typedef union {
uint32_t m_uint;
uchar_t m_uchar[4];
} MyNumberT;
uint32_t exchange_byte(uint32_t number, int from, int to) {
MyNumberT mynum;
mynum.m_uint = number;
uchar_t n = mynum.m_uchar[to];
mynum.m_uchar[to] = mynum.m_uchar[from];
mynum.m_uchar[from] = n;
return mynum.m_uint;
}
uint32_t swap_byte(uint32_t number, int byteno) {
MyNumberT mynum;
mynum.m_uint = number;
uint16_t n = (uint16_t) mynum.m_uchar[byteno];
n <<= 4;
uchar_t nn = (uchar_t) n;
n >>= 8;
nn |= (uchar_t) n;
mynum.m_uchar[byteno] = nn;
return mynum.m_uint;
}
int main() {
uint32_t number = 0x1baacc8b;
number = exchange_byte(number, 3, 1); // 0xccaa1b8b
number = exchange_byte(number, 2, 0); // 0xcc8b1baa
number = swap_byte(number, 1); // 0xcc8bb1aa
cout << hex << number << dec << endl;
return 0;
}