c# - Combine two bytes to short using left-shift -


i have high byte , low byte convert short.

i have implemented this, seems work, bit confused on why. both high_byte , low_byte cast bytes.

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

in code, should high_byte << 8 zero? tried this:

(byte)1 << 8 

which equals 256, thought should 0. guess missing something.

could please explain?

from c# language specification, section 4.1.5:

the integral-type unary , binary operators operate signed 32-bit precision, unsigned 32-bit precision, signed 64-bit precision, or unsigned 64-bit precision:

...

for binary << , >> operators, left operand converted type t, t first of int, uint, long, , ulong can represent possible values of operand. operation performed using precision of type t, , type of result t.

that is, whenever apply operators integral types in c#, result minimum of 32-bits. there other rules (given in ...) other operators, define how final types determined.


(as aside, i'd have thought important enough mention in c# reference i'm blowed if can find in there anywhere)


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -