Bitwise rotation (Circular shift)

前端 未结 5 1793
粉色の甜心
粉色の甜心 2021-01-05 08:04

I was trying to make some code in C++ about “bitwise rotation” and I would like to make this by the left shif. I didn’t know how to code this, but I found a little code in “

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 08:29

    if you want bitwise rotation over an arbitrary number of bits (for example 4), simply add a parameter to your function:

    unsigned int rotl(unsigned int value, int shift, unsigned int width) {
        return ((value << shift) & (UINT_MAX >> (sizeof(int) * CHAR_BIT - width))) | (value >> (width - shift));
    }
    

提交回复
热议问题