循环移位

sim*_*ion 1 c bit-shift

我必须将int向右移动一个位置并将其返回

在Java中我只能返回n >> 1;

这可能在C?

我们给出的方法如下

// Return n after a right circular 1-bit shift
unsigned int right_circular_shift_1(unsigned int n) {
Run Code Online (Sandbox Code Playgroud)

小智 10

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
Run Code Online (Sandbox Code Playgroud)

对于正确的循环移位:

- get the current rightmost bit  and save it
- shift the number rightwards by one
- or the saved bit in at the leftmost bit position
Run Code Online (Sandbox Code Playgroud)


Joh*_*web 5

你没试过吗?

n >> 1
Run Code Online (Sandbox Code Playgroud)

这将在C(和其他基于C语言)中工作,就像在Java中一样(尽管不是循环的).