开发者

How do I perform an unsigned right shift (>>> in Java) in C/C++?

开发者 https://www.devze.com 2022-12-22 13:05 出处:网络
How开发者_运维百科 do I perform an unsigned right shift (>>> in Java) in C/C++?In C, to get an unsigned shift, you just do a shift on an unsigned type.

How开发者_运维百科 do I perform an unsigned right shift (>>> in Java) in C/C++?


In C, to get an unsigned shift, you just do a shift on an unsigned type.

unsigned int result = (unsigned int)valueToBeShifted >> shiftAmount;

Note that there is no guarantee that >> on a signed type gives you a signed shift in C -- this is implementation defined behavior. Most common implementations produce a signed shift if the type is signed, however.


>>> is unsigned right shift, so I would think that in C this would be the same as

unsigned int foo;
unsigned int bar = foo >> whatever;
0

精彩评论

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