# Bit shifting << 16

I try to do a little stitching of bytes by shifting. Here is my example code:

long a = 0;
a = 1 << 16;
Serial.print(a, BIN);

So a should be = 65.563 afterwards imho.
My Serial.print tells me that a= 0;

When i try 1 << 15; everything is ok; then a= 256;

long has a size of 32 bit, so the << 16 command fits in.
I am confused. Has someone an idea what is going on?
I use a Arduino Mega 2560.

So a should be = 65.563 afterwards imho.

In the land of 32 bit "int"s your opinion would be valid.
But you’re in the land of 16 bit "int"s, so it ain’t.
(Though the value would be 65536)

Explanation `a = 1 << 16;`
1 is an “int”, 16 is also an “int”
Try `a = 1L << 16;`

In the land of 32 bit "int"s your opinion would be valid. But you're in the land of 16 bit "int"s, so it ain't.

You've got me, I come from the PC land.

(Though the value would be 65536)

Yes, of course I meant 65536, damn decimal separator. In german its quite the opposite: "." for the thousand separator and "," for decimal separator.

Thank you very much.

Separator or not, it would never be

65563

I would think this should also work on the Arduino Mega 2560 (tested on the DUE):

``````long a, b, c, d = 0;

void setup() {
Serial.begin(115200);
}

void loop() {
a = 1 << 16;
b = 1 * 65536;
c = 1 << 15;
d = 1 * 32768;

Serial.print ("a = ");  Serial.println(a, BIN);
Serial.print ("b = ");  Serial.println(b, BIN);
Serial.print ("c = ");  Serial.println(c, BIN);
Serial.print ("d = ");  Serial.println(d, BIN);

while (1);
}
``````

Results:

I would think this should also work on the Arduino Mega 2560 (tested on the DUE)

The Due lives in 32 bit land.

``````long a = 1;

void setup() {
Serial.begin(115200);
}

void loop() {
a <<= 16;
//etc
``````

The Due lives in 32 bit land.

True, but there's nothing specific to 32-bit in the code that I can see ... shouldn't something like this be portable? All I can find mentioned in the Arduino reference is that float and double is 8 bytes (64-bit on the DUE) and 4 bytes on AVR. Besides, I thought "short" was 16-bit and "long" 32-bit for both AVR and SAM.

All I can find mentioned in the Arduino reference is that float and double is 8 bytes

I'd read that bit again, if I were you.

long is 32 bit, but 1 is an "int", whatever size an "int" happens to be.

Yeah, messed up on the float size (4 bytes). However, from this comparison, "long" is the same on the DUE, UNO and ATMega 2560.

A "long" is 32 bits on a Due, Uno, Mega, Gallieo, Tre, Leonardo, Robot, Nano, Micro yadda yadda. However a 1, as I've already pointed out, is an "int".

OK, got it ... should explicitly define the width of "int" number (thanks +1).