疑问:一道基础的移位题

ZaiJianBa 2008-04-21 09:43:35

DWORD dwHigh = 0x01;
DWORD dwLow = 0x02;
unsigned __int64 res= 0;
res= (dwHigh<<32)|dwLow ;

不要看运行结果,问res的结果是多少?
...全文
227 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZaiJianBa 2008-04-25
  • 打赏
  • 举报
回复
万分感谢
ZaiJianBa 2008-04-23
  • 打赏
  • 举报
回复
谢谢楼上的各位

我自己是这样理解的
DWORD a = 0xFFFF0000;
a<<32;
对应的汇编指令是shl eax,20h
32 位的机器上, shl或者shr 32 bit,硬件指令系统将会做取模运算,然后再移动, 所以左移和右移 32bit 是没有意义的
unsigned __int64 res = a<<32;
相当于只是把eax的值赋给res;

而res = (unsigned __int64)a <<32

在VC下对应的指令是call __allshl(MS自定义的函数?????)
最后会将eax,edx的值赋给res.



Yun0825 2008-04-23
  • 打赏
  • 举报
回复
那么对一个整类型数据的左移n位,就是对应的二进制补码左边n位舍去,右边补n个零?
那么这个n 的大小是有限制的意思吗?
比如:DWORD就不能直接移32位(包括32)以上吗?
ForestDB 2008-04-23
  • 打赏
  • 举报
回复
LZ已经接近答案了,__allshl是MS写的汇编code,用来处理64位int的移位问题,目前的Intel的指令集是只能移32位int的.
ForestDB 2008-04-23
  • 打赏
  • 举报
回复
所以深蓝大道提到5是完全正确的.
ForestDB 2008-04-23
  • 打赏
  • 举报
回复
Intel Architecture Software Developer’s Manual, Volume 2: Instruction Set Reference
中的一段解释
SAL/SAR/SHL/SHR—Shift (Continued)
Description
These instructions shift the bits in the first operand (destination operand) to the left or right by
the number of bits specified in the second operand (count operand). Bits shifted beyond the
destination operand boundary are first shifted into the CF flag, then discarded. At the end of the
shift operation, the CF flag contains the last bit shifted out of the destination operand.
The destination operand can be a register or a memory location. The count operand can be an
immediate value or register CL. The count is masked to five bits, which limits the count range
to 0 to 31.
A special opcode encoding is provided for a count of 1.
里面有提到count被5bit masked。
ForestDB 2008-04-23
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20070817/09/61e5faf7-334f-4f29-bc28-7616d2813f18.html
lsmdiao0812 2008-04-22
  • 打赏
  • 举报
回复
32位平台移位运算只取低五位,应该明白了吧

//////////////
真的不明白,那位解释一下
Yun0825 2008-04-22
  • 打赏
  • 举报
回复
没明白
struggle813 2008-04-22
  • 打赏
  • 举报
回复
32位平台移位运算只取低五位,应该明白了吧
ninesuns02 2008-04-22
  • 打赏
  • 举报
回复
DWORD为32为,移动32位后又转到最低位为1
故(dwHigh) < < 32=1;
res=3
如果dwHigh < <31位,结果就是res=2^31+2
ninesuns02 2008-04-22
  • 打赏
  • 举报
回复
DWORD为32为,移动32位后又转到最低位为1
故(dwHigh) << 32=1;
res=3
如果dwHigh<<31位,结果就是res=2^32+2
struggle813 2008-04-22
  • 打赏
  • 举报
回复
3
struggle813 2008-04-22
  • 打赏
  • 举报
回复
DWORD a=0xffff0000;二进制=11111111111111110000000000000000
printf("%u", (a<<31)<<1 );
右移位15以上都是零,零再移也是零.

另外,大家交流,不要用什么过激的语言.
l999358 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 l999358 的回复:]
引用 1 楼 sun3411 的回复:
2呀.

同意,楼主的代码和

C/C++ code
DWORD dwHigh = 0x01;
DWORD dwLow = 0x02;
DWORD fuck;
unsigned __int64 res = 0;
fuck = (dwHigh<<32)|dwLow ;
res = (unsigned __int64) fuck;



是一样的
[/Quote]
我是错的,是3,但不是因为什么狗屁只用5位的事。
32位的数据,移32位,是非法操作,所以数据不会发生变化。。。。。。
不信你试验下:

DWORD a=0xffff0000;
printf("%u",a<<32);

结果就是0xffff0000

DWORD a=0xffff0000;
printf("%u", (a<<31)<<1 );

结果就是0
l999358 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sun3411 的回复:]
2呀.
[/Quote]
同意,楼主的代码和

DWORD dwHigh = 0x01;
DWORD dwLow = 0x02;
DWORD fuck;
unsigned __int64 res = 0;
fuck = (dwHigh<<32)|dwLow ;
res = (unsigned __int64) fuck;

是一样的
thch214 2008-04-22
  • 打赏
  • 举报
回复
高了点
我是风 2008-04-21
  • 打赏
  • 举报
回复
结果:4294967298
作如下修改:
res = ((unsigned __int64)(dwHigh) << 32) | dwLow;
sun3411 2008-04-21
  • 打赏
  • 举报
回复
2呀.

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧