본문 바로가기

Subject/Programming

Python] ROL, ROR

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))


>결과




'Subject > Programming' 카테고리의 다른 글