simion
simion

Reputation: 561

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

Upvotes: 1

Views: 2861

Answers (2)

anon
anon

Reputation:

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

Upvotes: 10

johnsyweb
johnsyweb

Reputation: 141810

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

Upvotes: 5

Related Questions