Bitwise Rotate Right

后端 未结 5 2090
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-06 08:55

I am trying to convert this C function into Python;

typedef unsigned long var;
    /* Bit rotate rightwards */
    var ror(var v,unsigned int bits) {
        ret         


        
5条回答
  •  自闭症患者
    2021-02-06 09:10

    The shortest way I've found in Python: (note this works only with integers as inputs)

    def ror(n,rotations,width):
        return (2**width-1)&(n>>rotations|n<<(width-rotations))
    

提交回复
热议问题