左移运算符

https://msdn.microsoft.com/en-us/library/a1sway8w.aspx

The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand.

The type of the second operand must be an int or a type that has a predefined implicit numeric conversion to int.

Remarks

If the first operand is an int or uint (32-bit quantity), the shift count is given by the low-order five bits of the second operand. That is, the actual shift count is 0 to 31 bits.

If the first operand is a long or ulong (64-bit quantity), the shift count is given by the low-order six bits of the second operand. That is, the actual shift count is 0 to 63 bits.

Any high-order bits that are not within the range of the type of the first operand after the shift are discarded, and the low-order empty bits are zero-filled. Shift operations never cause overflows.

User-defined types can overload the << operator (see operator); the type of the first operand must be the user-defined type, and the type of the second operand must be int. When a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly overloaded.

左移运算符返回的值,默认是int的

Comments

Note that i<<1 and i<<33 give the same result, because 1 and 33 have the same low-order five bits.

举例,比如我有一个数字0x 11 22 33 44 55 66

现在需要转换成十进制的数字

 var result = ((long)bytes[0]) << 40 | ((long)bytes[1]) << 32 | ((uint)bytes[2]) << 24 | (bytes[3]) << 16 | bytes[4] << 8 | bytes[5];

需要这么转换,否则存在数据位丢失的问题

原文地址:https://www.cnblogs.com/chucklu/p/4819251.html