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

后端 未结 2 2029
攒了一身酷
攒了一身酷 2021-01-01 11:49

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

2条回答
  •  有刺的猬
    2021-01-01 12:01

    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.

提交回复
热议问题