Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two bytes to short using left-shift

I have a high byte and a low byte I would like to convert to short.

I have implemented this, which seems to work, however I am a bit confused on why. Both high_byte and low_byte are cast as bytes.

short word = (short)(high_byte << 8 | low_byte);

In this code, should the high_byte << 8 be zero? Then I tried this:

(byte)1 << 8

which equals 256, which I thought should be 0. I guess I am clearly missing something.

Could someone please explain?

like image 580
jackfrost9p Avatar asked Jul 27 '15 13:07

jackfrost9p


1 Answers

From the C# language specification, section 4.1.5:

The integral-type unary and binary operators always operate with signed 32-bit precision, unsigned 32-bit precision, signed 64-bit precision, or unsigned 64-bit precision:

...

For the binary << and >> operators, the left operand is converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of the operand. The operation is then performed using the precision of type T, and the type of the result is T.

That is, whenever you apply any operators to integral types in C#, the result is always a minimum of 32-bits. There are other rules (given in the ...) for other operators, which define exactly how the final types are determined.


(As an aside, I'd have thought that this was important enough to mention in the C# Reference but I'm blowed if I can find it in there anywhere)

like image 178
Damien_The_Unbeliever Avatar answered Oct 03 '22 20:10

Damien_The_Unbeliever