Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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/1/php/278.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#.net和PHP中获得不同的结果_C#_Php_Vb.net_Bit Manipulation_Bitwise Operators - Fatal编程技术网

按位移位-在C#.net和PHP中获得不同的结果

按位移位-在C#.net和PHP中获得不同的结果,c#,php,vb.net,bit-manipulation,bitwise-operators,C#,Php,Vb.net,Bit Manipulation,Bitwise Operators,在PHP中运行此命令时,我得到: Code: 2269495617392648 >> 24 Result: 32 当我在C#.net或vb.net中运行它时,我得到: Code: 2269495617392648 >> 24 Result: 135272480 PHP是正确的 有趣的是,当我在.net中尝试移动任何大于int32的数字时,结果很糟糕 int32(2147483647)下的每个数字从php和c#.net或vb.net生成相同的结果 在.net中是否有解决

在PHP中运行此命令时,我得到:

Code: 2269495617392648 >> 24
Result: 32
当我在C#.net或vb.net中运行它时,我得到:

Code: 2269495617392648 >> 24
Result: 135272480
PHP是正确的

有趣的是,当我在.net中尝试移动任何大于
int32
的数字时,结果很糟糕

int32(2147483647)
下的每个数字从php和c#.net或vb.net生成相同的结果


在.net中是否有解决此问题的方法?

在移位前按位将您的号码与0xFFFFFF对齐

在Python中:


>>> 2269495617392648 >> 24
135272480L
>>> (2269495617392648 & 0xffffffff) >> 24
32L

我很少使用.net,但相当肯定语法会非常相似。

按位和您的数字在移位前使用0xFFFFFF

在Python中:


>>> 2269495617392648 >> 24
135272480L
>>> (2269495617392648 & 0xffffffff) >> 24
32L

我很少使用.net,但相当肯定语法会非常相似。

严格来说,PHP是错误的

编号
2269495617392648
的整个位模式为:

1000 0001 0000 0001 1000 0010 0000 0001 1000 0001 0000 0000 1000 (2269495617392648)
右移24次可以得到:

0000 0000 0000 0000 0000 0000 1000 0001 0000 0001 1000 0010 0000 (135272480)
这是
135272480
的位模式,而不是
32

Before truncation to 32-bits:
1000 0001 0000 0001 1000 0010 0000 0001 1000 0001 0000 0000 1000 (2269495617392648)

After truncation to 32-bits:
0010 0000 0001 1000 0001 0000 0000 1000 (538447880)

Right shifting the truncated bits by 24 bits:
0000 0000 0000 0000 0000 0000 0010 0000 (32)
PHP中的情况显然是,通过仅保留较低的32位,数字
2269495617392648
被截断为
538447880
。请注意,该数字
2269495617392648
太大,无法放入32位整数(有符号或无符号)

将截断的位右移24次,得到
32

Before truncation to 32-bits:
1000 0001 0000 0001 1000 0010 0000 0001 1000 0001 0000 0000 1000 (2269495617392648)

After truncation to 32-bits:
0010 0000 0001 1000 0001 0000 0000 1000 (538447880)

Right shifting the truncated bits by 24 bits:
0000 0000 0000 0000 0000 0000 0010 0000 (32)
当你说:

有趣的是,当我 尝试将任何数字移得大于 net中的int32会产生不好的结果

它会给您带来糟糕的结果,因为一些位被截断以适合32位


如果您正在从PHP移植到C#并且需要保留此行为,则需要使用
2269495617392648&0xffffffff
手动截断位,而不仅仅是
2269495617392648
(请参阅)。但是请注意,PHP代码中存在位截断问题。我不确定这是否是故意的。

严格来说,PHP是错误的

编号
2269495617392648
的整个位模式为:

1000 0001 0000 0001 1000 0010 0000 0001 1000 0001 0000 0000 1000 (2269495617392648)
右移24次可以得到:

0000 0000 0000 0000 0000 0000 1000 0001 0000 0001 1000 0010 0000 (135272480)
这是
135272480
的位模式,而不是
32

Before truncation to 32-bits:
1000 0001 0000 0001 1000 0010 0000 0001 1000 0001 0000 0000 1000 (2269495617392648)

After truncation to 32-bits:
0010 0000 0001 1000 0001 0000 0000 1000 (538447880)

Right shifting the truncated bits by 24 bits:
0000 0000 0000 0000 0000 0000 0010 0000 (32)
PHP中的情况显然是,通过仅保留较低的32位,数字
2269495617392648
被截断为
538447880
。请注意,该数字
2269495617392648
太大,无法放入32位整数(有符号或无符号)

将截断的位右移24次,得到
32

Before truncation to 32-bits:
1000 0001 0000 0001 1000 0010 0000 0001 1000 0001 0000 0000 1000 (2269495617392648)

After truncation to 32-bits:
0010 0000 0001 1000 0001 0000 0000 1000 (538447880)

Right shifting the truncated bits by 24 bits:
0000 0000 0000 0000 0000 0000 0010 0000 (32)
当你说:

有趣的是,当我 尝试将任何数字移得大于 net中的int32会产生不好的结果

它会给您带来糟糕的结果,因为一些位被截断以适合32位


如果您正在从PHP移植到C#并且需要保留此行为,则需要使用
2269495617392648&0xffffffff
手动截断位,而不仅仅是
2269495617392648
(请参阅)。但是请注意,PHP代码中存在位截断问题。我不确定这是否是有意的。

我上次使用C#已经有一段时间了,但我相信您的问题可能与PHP不是强类型的事实有关,它在运行时推断您的变量类型,因此如果您需要,它将为您提供一个大的变量类型。我认为C#中的变量正在溢出,所以使用更大的类型可能会有所帮助。C#中是否有int64类型?我上次使用C#已经有一段时间了,但我相信你的问题可能与PHP不是强类型的事实有关,它在运行时推断出你的变量类型,因此如果你需要的话,它会给你一个大的变量类型。我认为C#中的变量正在溢出,所以使用更大的类型可能会有所帮助。C#中是否有int64类型?要么那样,要么很长。