-
-
Save xenoscr/8d8d2787aae127eb0e608d5f708eece8 to your computer and use it in GitHub Desktop.
Quick binary rotation (rotl/rotr) in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rotl(num, bits): | |
bit = num & (1 << (bits-1)) | |
num <<= 1 | |
if(bit): | |
num |= 1 | |
num &= (2**bits-1) | |
return num | |
def rotr(num, bits): | |
num &= (2**bits-1) | |
bit = num & 1 | |
num >>= 1 | |
if(bit): | |
num |= (1 << (bits-1)) | |
return num | |
print rotl(255,8); | |
print rotr(255,8); | |
numr=1; | |
numl=1; | |
for i in range(0,10): | |
print "0x%02X (%d)" % (numr,numr) | |
print "0x%02X (%d)" % (numl,numl) | |
numr = rotr(numr,8) | |
numl = rotl(numl,8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment