Bit shift negative number ???

Hi

i am sending:

data[0] = 50;

Serial.println((data[0]<<14));
Serial.println((data[0]<<13));
Serial.println((data[0]<<12));
Serial.println((data[0]<<11));
Serial.println((data[0]<<10));
Serial.println((data[0]<<9));
Serial.println((data[0]<<8));
Serial.println((data[0]<<7));
Serial.println((data[0]<<6));
Serial.println((data[0]<<5));
Serial.println((data[0]<<4));
Serial.println((data[0]<<3));
Serial.println((data[0]<<2));
Serial.println((data[0]<<1));
Serial.println(data[0]);

and i am getting:

-32768
16384
8192
-28672
-14336
25600
12800
6400
3200
1600
800
400
200
100
50

Why am i getting the negative number??? :o

Welcome,

What are you trying to do? What do you expect as result? :)

If it helps, an "int" is a sixteen bit signed value on an eight bit Arduino.

Try adding this to the top of your code:

  unsigned int data[1];
  data[0] = 50;

Why an array instead of just data?

the reason I ask is that I put the 4 data together and can not figure out why I get the data back as I do
data [0] = 50;
data [1] = 56;
data [2] = 48;
data [3] = 68;
Serial.println ((data [0] << 12) + (data [1] << 8) + (data [2] << 4) + (data [3]));
the sum gives 23364 and that’s what I do not understand how the equation is.

It would be helpful if you used
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags.

Or, at least, disabled smileys.

You are getting negative numbers because of the way that twos-complement arithmetic works.

0000000000110010
0000000001100100
0000000011001000
0000000110010000
0000001100100000
0000011001000000
0000110010000000
0001100100000000
0011001000000000
0110010000000000
1100100000000000
1001000000000000
0010000000000000
0100000000000000
1000000000000000
0000000000000000

The negative numbers happen when that first bit - bit 15 - becomes 1. To get negative numbers in two’s complement arithmetic, the first bit is treated as having a value of -(2^n) rather than +(2^n). And that’s what’s going on here.