循环左移原理 拿一个32位的数(4个字节)来说 进行移动八位 如: 0x12345678 rol 8 之后 = 0x34567812 其原理如下: 1.首先左移八位得到 0x345678 2.然后右移24位得到 0x12 最后 0x345678 | 0x12 = 0x34567812 鉴于Python的特殊性.我们只需要32bit数即可. 也就是最后要 & 0xFFFFFFFF 其它移位同理
设你要移动的数 是一个 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 就可以 得到我们想要的数值的
上面说了下移位的原理那么这里介绍下Python代码的使用
以循环右移为例子 总共就分为下类三个函数
rorCustomByte 自定义定制字节移位
rorCustomBit 自定义Bit移位
ror8bit 按照8位数值 进行移位
下面说下什么意思
__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