Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 位移位-将值保留为字节_C#_Byte_Bit Shift - Fatal编程技术网

C# 位移位-将值保留为字节

C# 位移位-将值保留为字节,c#,byte,bit-shift,C#,Byte,Bit Shift,假设我有以下字节: byte myByte = 0xff; // 1111 1111 eg 255 我想向左移动2位: int newNumber = myByte << 2; int newNumber=myByte将结果强制转换为一个字节。应该这样做 或: int-newNumber=((byte)(myByte)(myByte@MarcB:&&是一个逻辑运算符,而不是按位运算符。@MarcB-(myByte是的,因为您正在执行int-newNumber)。编译器完全按照

假设我有以下字节:

 byte myByte = 0xff; // 1111 1111 eg 255
我想向左移动2位:

 int newNumber = myByte << 2;

int newNumber=myByte将结果强制转换为一个字节。应该这样做

或:


int-newNumber=((byte)(myByte
)(myByte@MarcB:
&&
是一个逻辑运算符,而不是按位运算符。@MarcB-
(myByte是的,因为您正在执行
int-newNumber
)。编译器完全按照您的要求执行操作。&0xFF将简单地屏蔽位8->15,因此您仍然只有8个有用位(0->7)在值中是的,您是正确的-我在OP中没有提到我希望返回一个
字节
。感谢您的帮助。第二个答案仍然生成一个
int
,只有值是字节范围。我只是解决OP的请求,删除两个高阶位。
var result = newNumber & 0xFF
int newNumber = ((byte)(myByte << 2));