circular shift c

雨燕双飞 提交于 2019-12-08 06:32:54

问题


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) {

回答1:


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



回答2:


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).



来源:https://stackoverflow.com/questions/2943265/circular-shift-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!