How do I perform a circular rotation of a byte?

后端 未结 3 1743
情深已故
情深已故 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:04

    There is no rotation operator in C, but if you write:

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

    then, according to this: http://www.linux-kongress.org/2009/slides/compiler_survey_felix_von_leitner.pdf (page 56), compilers will figure out what you want to do and perform the rotation it in only one (very fast) instruction.

提交回复
热议问题