How do I perform a circular rotation of a byte?

后端 未结 3 1748
情深已故
情深已故 2020-12-16 06:25

I\'m trying to implement a function that performs a circular rotation of a byte to the left and to the right.

I wrote the same code for both operations. For example,

3条回答
  •  心在旅途
    2020-12-16 07:31

    If, as according to your comments, you want to shift one bit exactly, then one easy way to accomplish that would be this:

    unsigned char rotl(unsigned char c)
    {
        return((c << 1) | (c >> 7));
    }
    

    What your code does is reversing the bits; not rotating them. For instance, it would make 10111001 into 10011101, not 01110011.

提交回复
热议问题