I encountered a problem when trying to manipulate the bits in the High Word (Bits 31-16) of a uint32_t value (unsigned integer, 32 bits of length), using bitwise operators. I have an Arduino Mega2560.
uint8_t should be the same as unsigned byte (min value:0, max value: 255)
please try executing the following code and tell me what happens:
uint8_t byte0 = 0xFE; // least significant byte
uint8_t byte1 = 0xE9;
uint8_t byte2 = 0x9E;
uint8_t byte3 = 0x46;
uint32_t test, hwlb, hwhb; // High Word Low Byte, High Word High Byte
hwlb = byte2;
hwhb = byte3;
Serial.println( hwlb << 16, HEX); // outputs 0x009E0000, which is correct
Serial.println( hwhb << 24, HEX); // outputs 0x46000000, which is fine too
test |= byte0;
test |= byte1 << 8;
test |= hwlb << 16;
Serial.println(test, HEX); // outputs 0xFFFFE9FE, which is strange, 'cause only Byte0 and Byte1 are correct
test |= hwhb << 24;
Serial.println(test, HEX); // outputs 0xFFFFE9FE, like above
(Why I created hwlb and hwhb to store the values of Byte2 and Byte3 ? Because I suspected that Arduino cannot bitshift a mere byte such a long way. Trying without these 2 variables got no better results. )
Does anyone have an idea what’s problematic about the operations test |= hwlb << 16; and test |= hwhb << 24; ???
Actually, the value of test should be 0x469EE9FE in the end, which is Byte0-3 rowed up.
Feel free to try other byte values as well and share your results with me
Thanks in advance !!