How do I perform an unsigned right shift (>>> in J

2020-02-10 02:44发布

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

2条回答
够拽才男人
2楼-- · 2020-02-10 03:05

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.

查看更多
姐就是有狂的资本
3楼-- · 2020-02-10 03:22

>>> 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;
查看更多
登录 后发表回答