Left shift count >= width of type [-Wshift-count-overflow]

So, i’m getting the titled error message:
left shift count >= width of type [-Wshift-count-overflow]

My program seems to compile and run fine, but i wonder why i’m getting the message, seeing as i’m only shifting 16 in an unsigned 32bit int.

I’ve got a couple of rather similar functions with the little green underline:

uint32_t BMP280i2c::testReaderMultiLine(){

uint32_t data = Wire.read();
data |= (Wire.read() << 8);
data |= (Wire.read() << 16);

return data;

Well shifting a 32bit type 16 times to the left results in a type that should have at least 48bit if you don’t want to lose any information (a 0x80000000 will be a 0x800000000000). As you do this on purpose you probably don’t mind the loss of information and you can ignore the warning. If you want to get rid of the warning you would have to cast the return value of Wire.read() to a bigger type and also change data to be a bigger type.

Seems i’ve misunderstood how shifting and/or how reading from i2c works, which seems odd since my functions working with smaller shifts work fine.

I am making the assumption that Wire.read() returns a single byte and that:
Say i have 2 bytes of data, “b1” and “b2”, and a 16 bit int “data”
b1 = 01101001
b2 = 10010110

if i say data = b1 i expect data to be: 00000000 01101001

if i then say data |= (b2 << 8) i expect data to be: 10010110 01101001

Is this wrong?

Well I asumed Wire.read() returns 32bits because your code said uint32_t data = Wire.read();.
If it only returns 8bits then the size of data is fine but the problem with shifting remains. I think you get the order wrong in which the shifting happens.
Lets assume

data = (Wire.read() << 8);

then the shifting will happen first. You shift a byte, let’s say 11111111b 8 times to the left. The result will still be a byte so you will have lost all your information and end up with 00000000b. AFTER that those 8bits will be written to the lager variable data.

In your example, after data = b1 data will be 00000000 01101001, yes.
The problem comes in the second line where the shift will happen first. b2 will end up beeing 00000000b because you “shifted out” all the information into nirvana ;). You then bitwise OR it with data and end up with 00000000 01101001.

To solve that you will have to cast b1 and b2 into bigger types BEFORE your shift them, i.e.

uint32_t data = Wire.read();
data |= ((uint32_t) Wire.read() << 8);
data |= ((uint32_t) Wire.read() << 16);

Yeah, sorry i should have specified that Wire.read() spits out a single byte.

I hadn’t considered that at all, that i was shifting the 8bit value inside its 8bit box before putting it into the 32bit box, i just assumed that i was passing a value equal to the shifted value as if the original data had been in a bigger box, since the original data isn’t changed.

I think it makes sense, and my code is yet again warning-free.

Thanks again Thomseeen!