Python精簡代碼實現循環左移循環右移


Python實現循環左移右移

一丶Python實現循環左移 右移 原理

1.1 Python實現循環左移

1.1.1 文字原理介紹

循環左移原理
拿一個32位的數(4個字節)來說 進行移動八位
如:
0x12345678 rol 8 之后 = 0x34567812
其原理如下:
1.首先左移八位得到 0x345678
2.然后右移24位得到 0x12
最后 0x345678 | 0x12 = 0x34567812
鑒於Python的特殊性.我們只需要32bit數即可. 也就是最后要 & 0xFFFFFFFF
其它移位同理

1.2 Python實現循環右移

1.2.1 右移位原理介紹

​ 設你要移動的數 是一個 4字節(32bit)的數 要移動八位
則原理如下:
0x12345678 ror 8(bit) = 0x78123456

1.首先得到 123456 也就是 >> 8位即可得到
2.然后得到 78 也就是 << 24位即可得到
得到的兩個值進行|運算
也就是 0x78 | 0x 123456 = 0x78123456
0x78
| 0x 123456
= 0x78123456
但是python的特殊性 你移位之后數值不會溢出,所以我們只需要相應位數的字節即可. 也就是我們只要32位(4個字節) 最后&0xFFFFFFFF 就可以
得到我們想要的數值的

二丶代碼示例

2.1 代碼介紹

上面說了下移位的原理那么這里介紹下Python代碼的使用

以循環右移為例子 總共就分為下類三個函數

rorCustomByte       自定義定制字節移位 
rorCustomBit        自定義Bit移位
ror8bit             按照8位數值 進行移位

下面說下什么意思

  • rorCustomByte 自定義定制字節移位

    總共三個參數

    參數1: 你想移動的數值

    參數2: 字節分組 按照4bit一個分組 說明一下參數1 是多少個分組組成 如 0x12345678 有8個分組 每組4個bit

    參數3: 你要移動多少個字節

    使用代碼如下

    v = 0x12345678             有一個數值是0x12345678 是一個四字節表示的值 也就是32位
    print(hex(ror.rorCustomByte(v, 8, 4))) 這個函數的作用則是 這個V值有8個分組,每組分為4bit (合起來是32bit) 然后循環移位4個分組(16位)
    輸出結果如下:
    >>> 0x56781234
    
    繼續使用
    v = 0x1234
    print(hex(ror.rorCustomByte(v, 4, 2)))
    >>> 0x3412
    
  • rorCustomBit 按照bit來進行移位

    這個移位很明白了.

    參數1: 你要移動的數值

    參數2: 你移動的這個數值是多少位

    參數3: 你要移動多少位

    使用代碼如下:

    v = 0x12345678
    print(hex(ror.rorCustomBit(v,32,4)))  參數2指明了你要移動的v值是一個32bit的值. 你要移動4位. 
    v = 0x1234
    print(hex(ror.rorCustomBit(v, 16, 4))) 同上
    
    輸出值:
    >>> 0x81234567
    >>> 0x4123
    
  • ror8bit 是一個分類函數.是說明你要把一個一個數按照8bit的方式來進行移位 當然也有ror4bit ror16bit .....

    參數1: 你要移動的數值

    參數2: 你要移動多少位

    其實rorCustomBit 就是根據此類函數改造的.可以定制移動.

    使用如下:

    v = 0xC
    print(hex(ror.ror4Bit(v, 1)))
    v = 0x12
    print(hex(ror.ror8Bit(v, 4)))
    v = 0x1234
    print(hex(ror.ror16Bit(v, 4)))
    v = 0x12345
    print(hex(ror.ror20Bit(v, 4)))
    v = 0x123456
    print(hex(ror.ror24Bit(v, 4)))
    v = 0x1234567
    print(hex(ror.ror28Bit(v, 4)))
    v = 0x12345678
    print(hex(ror.ror32Bit(v, 4)))
    分別輸出:
    0x6
    0x21
    0x4123
    0x51234
    0x612345
    0x7123456
    0x81234567
    

    實現循環左移同理. 都是三類函數

    from LearnPy.Py1.RorOpt import AsmUtils
    
    ror = AsmUtils.RorObj()
    rol = AsmUtils.RolObl()
    v = 0xC
    print(hex(rol.rol4Bit(v, 1)))
    v = 0x12
    print(hex(rol.rol8Bit(v, 4)))
    v = 0x1234
    print(hex(rol.rol16Bit(v, 4)))
    v = 0x12345
    print(hex(rol.rol20Bit(v, 4)))
    v = 0x123456
    print(hex(rol.rol24Bit(v, 4)))
    v = 0x1234567
    print(hex(rol.rol28Bit(v, 4)))
    v = 0x12345678
    print(hex(rol.rol32bit(v, 4)))
    
    

2.2 完成Python代碼

__author__ = 'IBinary blob https://www.cnblogs.com/ibinary/'

class RorObj():
    def __init__(self):
        pass
    #字節循環移位
    #參數1 要移動的值
    #參數2 值是多少個字節
    #參數3 要移動的字節位數
    def rorCustomByte(self,v, Byte, shiftByte):
        shiftByte = (shiftByte * 4) & (Byte * 4 - 1)  # 按照bit值 來進行設置移動位數
        if shiftByte == 0:
            return v
        a1 = (v >> (shiftByte))  # 右移shift位 空出高位shift位
        a2 = (v << ((Byte * 4) - shiftByte))  # 計算出剩下要移動的位數
        _ = '0x' + 'F' * Byte
        FfValue = int(_, 16)
        value = (a2 | a1) & FfValue
        return value

        # 循環右移n位
        # 參數1 要移動的值
        # 參數2 你的這個是是多少位的. 比如 0x12345678 就是32位
        # 參數3 你要移動多少位 比如 4位  那么移動之后就是 0x81234567

    def rorCustomBit(self, v, Bytebit, shift):
        shift &= (Bytebit - 1)  # 按照bit值 來進行設置移動位數
        if shift == 0:
            return v
        a1 = (v >> shift)  # 右移shift位 空出高位shift位
        a2 = (v << ((Bytebit) - shift))  # 計算出剩下要移動的位數
        l = [x for x in range(4, Bytebit + 1) if x % 4 == 0]
        LCount = len(l)
        _ = '0x' + 'F' * LCount
        FfValue = int(_, 16)
        value = (a2 | a1) & FfValue
        return value
    def ror4Bit(self, v, shift):
        shift &= 3
        if shift == 0:
            return v
        return ((v >> shift) | (v << (4 - shift))) & 0xF
    def ror8Bit(self,v, shift):
        shift &= 7
        if shift == 0:
            return v
        return ((v >> shift) | (v << (8 - shift))) & 0xFF
    def ror12Bit(self,v, shift):
        shift &= 11
        if shift == 0:
            return v
        return ((v >> shift) | (v << (12 - shift))) & 0xFFF
    def ror16Bit(self,v, shift):
        shift &= 15
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (16 - shift))) & 0xFFFF
    def ror20Bit(self,v, shift):
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (20 - shift))) & 0xFFFFF
    def ror24Bit(self,v, shift):
        shift &= 23
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (24 - shift))) & 0xFFFFFF
    def ror28Bit(self,v, shift):
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (28 - shift))) & 0xFFFFFFF
    def ror32Bit(self,v, shift):
        shift &= 0x1F  # 設置要移動的位數
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (32 - shift))) & 0xFFFFFFFF
class RolObl():
    def __init__(self):
        pass

    # 循環左移原理
    # 拿一個32位的數(4個字節)來說 進行移動八位
    # 如:
    # 0x12345678  rol 8 之后  = 0x34567812
    # 其原理如下:
    # 1.首先左移八位得到 0x345678
    # 2.然后右移24位得到 0x12
    # 最后 0x345678 | 0x12  = 0x34567812
    # 鑒於Python的特殊性.我們只需要32bit數即可. 也就是最后要 & 0xFFFFFFFF
    # 其它移位同理
    def rolCustomBit(self,v,bit,shift):
        shift &= (bit-1)
        if shift == 0:
            return v
        HightBit = v >> (bit - shift)
        LowBit = v << shift
        l = [x for x in range(4, bit + 1) if x % 4 == 0]
        LCount = len(l)
        _ = '0x' + 'F' * LCount
        FfValue = int(_, 16)

        Value = (HightBit | LowBit) & FfValue
        return Value

    #按照字節移位
    def rolCustomByte(self,v,Byte,shiftByte):
        shiftByte = (shiftByte * 4) & (Byte * 4 - 1)  # 按照bit值 來進行設置移動位數
        if shiftByte == 0:
            return v
        Low = (v << (shiftByte))  #左移shift位
        Hight = (v >> ((Byte * 4) - shiftByte))  # 計算出剩下要移動的位數
        _ = '0x' + 'F' * Byte
        FfValue = int(_, 16)
        value = (Hight | Low) & FfValue
        return value

    def rol4Bit(self,v,shift):
        shift &= 3
        if shift == 0:
            return  v
        HightBit = v >> (4 - shift)
        LowBit =  v << shift
        Value = (HightBit | LowBit) & 0xF
        return Value
    def rol8Bit(self,v,shift):
        shift &= 7
        if shift == 0:
            return  v
        HightBit = v >> (8 - shift)
        LowBit =  v << shift
        Value = (HightBit | LowBit) & 0xFF
        return Value

    def rol12Bit(self, v, shift):
        shift &= 11
        if shift == 0:
            return v
        HightBit = v >> (12 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFF
        return Value

    def rol16Bit(self, v, shift):
        shift &= 15
        if shift == 0:
            return v
        HightBit = v >> (16 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFFF
        return Value

    def rol20Bit(self, v, shift):
        if shift == 0:
            return v
        HightBit = v >> (20 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFFFF
        return Value

    def rol24Bit(self, v, shift):
        shift &= 23
        if shift == 0:
            return v
        HightBit = v >> (24 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFFFFF
        return Value

    def rol28Bit(self, v, shift):
        if shift == 0:
            return v
        HightBit = v >> (28 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFFFFFF
        return Value
    def rol32bit(self,v,shift):
        shift &= 0x1F
        if shift == 0:
            return v
        HightBit = v >> (32 - shift)
        LowBit = v << shift
        value = (HightBit | LowBit) & 0xFFFFFFFF
        return value


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM