Where two 16 bit ADC values are merged into one 32 bit cell using bitshift (I am using an esp32, so it's quicker to send data this way).
I also have an additional 2 channels that records a timestamp at channel 0 each timestep.
Here is the code (N.B. just using arbitary values to fill array). All prints are for debugging / printing array:
I expect to see all cells populated with 11111111111111111111111111111111
When printing each time it populates a cell in the array I get: 1111111111111110000000000000000 after bitshift then 11111111111111111111111111111111 after adding secondary 16 bit to cell which is as expected.
@willpowell Can you elaborate a little on what your application is supposed to do? Maybe there are easier ways to achieve your goal. Are you sure you actually need all this bit shifting, for instance?
As it's now working , clearly you are right to say it's not a problem! But, I suspect, not for the reason you think...
If you ran this code on an 8-bit MCU, it would not work, I suspect. The reason that it does work ok is not because you are writing the results to a 32-bit variable, it's because you are running it on a 32-bit MCU. Sounds like a subtle distinction, I realise, but a potentially important one. On an 8-bit MCU, the result of "adcSample1 << 16" would be treated as a 16-bit value and bits would be lost. But on a 32-bit MCU, the result of "adcSample1 << 16" would be treated as a 32-bit value and no bits are lost.
The question is really about what happens when you shift a 16-bit value by 16 bits on a 32-bit MCU Vs an 8-bit MCU. Could be any value, does not have to be from an ADC.
It's not the MCU, it's the compiler's choice of word size for 'int'. If you use int32_t it should be the same on either an 8 bit or 32 bit processor (16 or 32 bit 'int').