bit shift left

Hi, i've been having some errors while bit shifting left with a long, here's the test code i've been running:

void setup() {
  long var = 0;
  Serial.begin(9600);
  
  var = (0x11 << 16) + (0x22 << 8) + (0x33 << 0);
  Serial.print(var, HEX);
}

void loop() {}

I'm supposed to get 0x112233 printed out on the serial monitor, but instead i get 0x2233
I managed to fix it with this line:

  var = (0x11 * pow(2,16)) + (0x22 << 8) + (0x33 << 0);

I'd still like to know why the bit shift left isn't working here. Thanks

The number 0x11 might be used as a 16-bit integer.

I'm not sure how to declare a long, I know 17L is a long.

You can do this:
long L1 = 17L;
L1 <<= 16;

Or do you want the preprocessor to do the shifting ?

There is another option. When the offset is always 24, or 16 or 8, you could create a union with a long and a byte array, and you can write it directly into the position you want.

Literal number constants are interpreted as ints (which are 16-bit on AVRs). When you shift an int 16 bits, you overflow it and there's nothing left in the value.

Try this:

var = (0x11L << 16) + (0x22 << 8) + (0x33 << 0);

oh! that explains it,
so if the values were byte variables, i'd first have to store them into a long then bit shift them?

Or cast it to a long.

char byte1;
long long1;

long1 = (long)byte1 << 16;