我必须将 int 向右移动一个位置并返回
在 Java 中,我可以只返回 n >> 1;
这在C中可能吗?
我们给出的方法如下
// Return n after a right circular 1-bit shift
unsigned int right_circular_shift_1(unsigned int n) {
C没有循环移位,所以我想练习是实现它。为左循环移位执行此操作的方法是:
- get the current leftmost bit and save it
- shift the number leftwards by one
- or the saved bit in at the rightmost bit position
对于右循环移位:
- get the current rightmost bit and save it
- shift the number rightwards by one
- or the saved bit in at the leftmost bit position
你没试过吗?
n >> 1
这将像在 Java 中一样在 C(和其他基于 C 的语言)中工作(但不是循环的)。