开发者

circular shift c

I have to shift the int one place to the right and return it

In Java i can just return n >> 1;

Is this possible 开发者_运维技巧in C?

The method we were given is as follows

// Return n after a right circular 1-bit shift
unsigned int right_circular_shift_1(unsigned int n) {


C does not have a circular shift, so I guess the exercise is to implement it. The way to do this for a left circular shift is to:

- get the current leftmost bit  and save it
- shift the number leftwards by one
- or the saved bit in at the rightmost bit position

For a right circular shift:

- get the current rightmost bit  and save it
- shift the number rightwards by one
- or the saved bit in at the leftmost bit position


Have you not tried it?

n >> 1

This will work in C (and other C-based languages) as it does in Java (not circular, though).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜