Bitwise Rotate Right

后端 未结 5 2034
佛祖请我去吃肉
佛祖请我去吃肉 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:24

    def rotation_value(value, rotations, widht=32):
        """ Return a given number of bitwise left or right rotations of an interger 
        value,
        for a given bit field widht.
        if rotations == -rotations:
            left
        else:
            right
        """
        if int(rotations) != abs(int(rotations)):
            rotations = widht + int(rotations)
        return (int(value)<<(widht-(rotations%widht)) | (int(value)>>(rotations%widht))) & ((1<

提交回复
热议问题