Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# uint中的位移位_C#_C# 4.0_C# 3.0_Bitwise Operators_Bit Shift - Fatal编程技术网

C# uint中的位移位

C# uint中的位移位,c#,c#-4.0,c#-3.0,bitwise-operators,bit-shift,C#,C# 4.0,C# 3.0,Bitwise Operators,Bit Shift,我无法使用“”获取0x8041 uint number = 0x418 in bits : 0000010000011000 uint number1 = 0x8041 in bits: 1000000001000001 uint number2 = 0x1804 in bits: 0001100000000100 或 如何使用shift从0x418获取0x8041和0x1804 解决方案 (number >> 4) & 0xffff; (number>>nbit

我无法使用“”获取0x8041

uint number = 0x418  in bits : 0000010000011000
uint number1 = 0x8041 in bits: 1000000001000001
uint number2 = 0x1804 in bits: 0001100000000100

如何使用shift从0x418获取0x8041和0x1804

解决方案

   (number >> 4) & 0xffff;
(number>>nbits)|(numberC#没有按位旋转运算符-经过右端移动的位会掉落并消失。要解决此问题,您可以执行以下操作:


(number>>nbits)|(number您所描述的通常称为旋转,而不是移位。在汇编(x86)中,这是通过ROR和ROL指令公开的

我不知道C#中有哪种按位运算符可以做到这一点,但算法非常简单:

(number >> nbits) | (number << (16 - nbits))
value=value&0x1?(1>1):(值>>1);

这个问题与旋转有什么关系?@Aniket好吧,看到OP对这个问题的编辑,这个答案正是他想要的。有关编译器友好的旋转的最佳实践,请参阅,以防计数为0或大于类型宽度时出现问题。在C#中应该同样有效。
(number >> nbits) | (number << (16 - nbits))
value = value & 0x1 ? (1 << Marshal.SizeOf(value) * 8 - 1) | (value >> 1) : ( value >> 1);