gilperon:
Does anybody knows how to solve this?
The solution is: Write code for what you want to do!
byte b = 190; //10111110
b= b<<8; // shift all 8 bits of the byte out to the left
Serial.println(b,BIN); // nothing left, all zero
And don't get confused with the default type conversions.
When you do something like:
Serial.println((some_expression),BIN);
Then (some_expression) is of type "int" by default.
The default numeric type in C is always "int".
So if you write that expression in your print command:
Serial.println(b << 8,BIN);
the C compiler will do several conversions and calculations before printing an "int" (and NOT a "byte").
The conversions are: Your byte 'b' will become a 'short', which is negative, then this negative 'short' variable will be put into an 'int' which will represent the same negative number. And with this int variable the actual shifting is done, and the resulting int will be printed.
So actually you wrote that:
Serial.println((int)(short(b))<<8,BIN);
As you are so astonished about the output, I think you didn't realize what you really wrote to be executed in the Serial.print command. So I'd recommend:
- calculate the desired result into a variable that you want to print
- print the variable
Dont't try compiler magic using other variable types than 'int' within the print command if you are clueless about C programming.