你觉得自己真的了解C中的移位运算吗?进来讨论一下.该如何处理

你觉得自己真的了解C中的移位运算吗?进来讨论一下...
我前几天写程序时,非常偶然的发现了一个有趣的现象,不知朋友们遇到过没有,是关于右移运算的。我对其进行了分析,试着做了解释,并写到了我的博客里,就当抛砖引玉了。  

欢迎大家提出更多高见!!  
欢迎大家指教!!  

如果哪位朋友提出更好的看法,我给加分^_^


http://blog.163.com/meng_luckywolf/blog/static/385458922007715102935939/

------解决方案--------------------
lz好像有问题阿。

我不知道什么根据最低5位,来决定与否。(lz这个需要确认哦) 但是以下是msdn上的
---------------------------------------------
1. 所有的位移操作的右操作数必须小于左操作数的位长度,否则结果未定义。
2. 右移操作对于unsigned系列,高位一直补0。对于signed系列,高位补符号位。
3. 在操作过程当中,有可能产生Integral Promotions。这就比较复杂了。C++中采用和C一样的策略,提升后的的量总是“保值的”,即原有的bit值不变;但不一定是“保号的”。运算中,如果char/bit field不能保持全部的值,就会被提升到int型,如果int也不能保存全部的值就会被提升至unsigned。有几种罕见的情况,保值和保号的运算会导致不同的值:
(1) /, %, /=, %=, <, <=, > , > = 运算依赖于符号,应用时可能导致不同结果。
(2)> > , > > = 运算有时依赖于符号位。
(3)函数重载参数可能依赖于符号。

------解决方案--------------------
> > 32 or > > shiftValue
in asm is SHR and
in machine code is C1 /5 ib or D3 /5

see http://www.xemean.net/resource/docs/doc3.htm

these 2 machine code only take 5 bit oprand.

LZ的想法是正确的,但不时Intel的bug,而是指令集的设计即是如此。

------解决方案--------------------
但a和b若是__int64的话,

> > 的汇编码是

0040B506 E8 65 FF FF FF call _aullshr (0040b470)


这是_aullshr的源码:
_aullshr PROC NEAR

;
; Handle shifts of 64 bits or more (if shifting 64 bits or more, the result
; depends only on the high order bit of edx).
;
cmp cl,64
jae short RETZERO

;
; Handle shifts of between 0 and 31 bits
;
cmp cl, 32
jae short MORE32
shrd eax,edx,cl
shr edx,cl
ret

;
; Handle shifts of between 32 and 63 bits
;
MORE32:
mov eax,edx
xor edx,edx
and cl,31
shr eax,cl
ret

;
; return 0 in edx:eax
;
RETZERO:
xor eax,eax
xor edx,edx
ret

_aullshr ENDP

end

------解决方案--------------------
首先为自己犯的一个错误更正:
/4 或者 /5不是取模的意思,而是用来分辨是左移还是右移的opcode。
:)有兴趣的自己去看intel instruction set。

再来:
在C中, < < or > > 被编译器转换成shl or shr的指令。
下面是
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。

另外跑到书店,在Intel microporcessors: Architecture, Programming and Interface中也有提到mod32的CL。