I'm trying to break a 2-byte number into two 1 byte numbers. But I get wrong result. Assumed number is: 0x1234H
uint8_t high = 0;
uint8_t low = 0;
high = static_cast<uint8_t >(val & 0xFF);
low = static_cast<uint8_t >(val >> 8);
cout << std::bitset<8>(high) << endl;
cout << std::bitset<8>(low) << endl;
cout << "high byte: " << static_cast<int >(high) << endl;
cout << "low byte: " << static_cast<int >(low) << endl;
When I run the code I expect to get the following output:
0x1234
00001100
00010010
high byte: 12
low byte: 34
Yet instead I get,
0x1234
00110100
00010010
high byte: 34
low byte: 12
Why do I fail in my attempt?