开发者

Right shift operator

开发者 https://www.devze.com 2023-02-14 23:44 出处:网络
I spent several hours today hunting for a b开发者_C百科ug until I found something I don\'t understand.

I spent several hours today hunting for a b开发者_C百科ug until I found something I don't understand.

This is the code I've been working with:

unsigned long k,l,m;
k = 1000;
l = 33;
m = k>>l;

It gives m=500 i.e. it apparently shifts by l mod 32!

I have logically expected 0.

Is this something I have just overlooked and never noticed?


In C, I believe it is not specified what will happen if you use shifts that are too large for the size of the value that you are shifting, so having the shift count wrap is perfectly acceptable (and is what x86 and IIRC PowerPC do). In Java, the wrapping behavior you saw is mandatory.


The ANSI C standard says:

If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

So, shifting by 33 isn't the same as 33 times shifting by one ...


Is there a possibility you are confusing 1(one) and l(letter L) as this has happened to me before !! (double check perhaps by changing x=33; m = k>>x ?)

0

精彩评论

暂无评论...
验证码 换一张
取 消