#include <iostream>
using std::cout;
using std::endl;
union Twobytes {
private:
unsigned short w;
unsigned char ch[2];
public:
unsigned short &word() {
return w;
}
unsigned char &lowByte() {
return ch[0];
}
unsigned char &highByte() {
return ch[1];
}
};
int main()
{
Twobytes w;
w.word() = 0x3210;
cout << "Word: " << (int)w.word() << endl;
cout << "lowByte: " << (int)w.lowByte() << endl;
cout << "highByte: " << (int)w.highByte() << endl;
return 0;
}
/*
run:
Word: 12816
lowByte: 16
highByte: 50
*/