본문 바로가기

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' 카테고리의 다른 글

AutoHotkey] 설명, 예제  (1) 2017.01.12
Python] For 반복문  (0) 2017.01.11
Python] 리스트  (0) 2017.01.11
notepadd++를 이용하여 C/C++ 컴파일하기  (0) 2017.01.09
JAVA] JFrame을 이용한 메모장 -4) About 창 만들기  (0) 2016.12.14