메모장 입니다2

파이썬] ROL, ROR 연산 본문

Study/Programming

파이썬] ROL, ROR 연산

Wooum@n 2017. 8. 8. 18:19

1. ROL


 -Rotate Left

 -Shift Left 연산에서, MSB 오버플로우시 LSB로 되돌아온다.

1001 0000  -> 0010 0001

 -소스 코드

#RotateLeft Function

def rotateLeft(x, n):

shiftBit = x << n

shiftBit &= 255

carryBit = x >> 8 - n

result= shiftBit | carryBit

return result


#main

ori=0x31


print "ori:{}".format(hex(ori))

print "bin:{}".format(bin(ori))


print "---------------------"

shfData=rotateLeft(ori, 5);


print "shifData:{}".format(hex(shfData))

print "bin:{}".format(bin(shfData))


>결과





2. ROR


 -Rotate Right
 -소스코드
#RotateRight Function

def rotateRight(x, n):

shiftBit = x >> n

carryBit = x << (8 - n)

carryBit &= 255

result= shiftBit | carryBit

return result


#main

ori=0x31


print "ori:{}".format(hex(ori))

print "bin:{}".format(bin(ori))


print "---------------------"

shfData=rotateRight(ori, 5);


print "shifData:{}".format(hex(shfData))

print "bin:{}".format(bin(shfData))


>결과