有关位移算子的问题 [英] Question on bit shift operators
问题描述
嗨大师
我是Bitwise Shift操作员的新手,我需要你的帮助以下代码
ulong unitDate =((( ulong ) 885478688974 )<< 32 )+( ulong ) 20170111 ;
uint low =( uint )(unitDate&( ulong ) uint .MaxValue);
uint high =( uint )(unitDate>> 32 跨度>);
低变量
我得到数据= 20170111这是正确的
但是在高变量中,我得到的值为715425998,这是不正确的,我应该得到值885478688974
谢谢
我的尝试:
如果使用下面的代码,我会得到低和高变量的确切值。如何处理大于4294967295的数据
ulong unitDate =((( ulong ) 4294967295 )<< 32 )+( ulong ) 20170111 ;
uint low =( uint )(unitDate&( ulong ) uint .MaxValue);
uint high =( uint )(unitDate>> 32 跨度>);
您的帮助将不胜感激
< blockquote class =quote>Quote:但是在高变量中我得到的值为715425998,这是不正确的
否,这是正确的,只需了解原因。
值885478688974适合40位,然后将其移位32位,结果应为72位但结果存储在64位变量中/ register。
这就是所谓的溢出,最后8位丢失。
[更新]
希望这更清楚。
您可以使用渡船穿越湖泊,该船可容纳32辆汽车。
您尝试安装40辆汽车。水中会有多少辆汽车?
这是你遇到的问题,这是不可能的。
Hi Gurus
I am very new to Bitwise Shift Operators ,I need your help in below code
ulong unitDate = (((ulong)885478688974) << 32) + (ulong)20170111;
uint low = (uint)(unitDate & (ulong)uint.MaxValue);
uint high = (uint)(unitDate >> 32);
in Low variable i am getting the data =20170111 which is correct
But in the high variable i am getting the value as 715425998 which is not correct i should get the value 885478688974
Thanks
What I have tried:
if go with below code i am getting exact values in low and high variable. how to handle the data which is bigger than 4294967295
ulong unitDate = (((ulong)4294967295) << 32) + (ulong)20170111;
uint low = (uint)(unitDate & (ulong)uint.MaxValue);
uint high = (uint)(unitDate >> 32);
your help would be appreciated
Quote:But in the high variable i am getting the value as 715425998 which is not correct
No, it is correct, just have to understand why.
The value 885478688974 fit in 40 bits, then you shift it 32 bits, the result should be 72 bits but the result is stored in a 64 bits variable/register.
That is what is named an overflow and last 8 bits are lost.
[Update]
Hope this make it clearer.
You use a ferryboat to cross a lake, the boat have space for 32 cars.
You try to fit 40 cars. How many cars will end in water ?
That is the problem you encounter, it is impossible.
这篇关于有关位移算子的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!