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 byte
s.
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 typet
,t
first ofint
,uint
,long
, ,ulong
can represent possible values of operand. operation performed using precision of typet
, , type of resultt
.
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
Post a Comment