Skip to content

Instantly share code, notes, and snippets.

@speters
Created October 12, 2016 17:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save speters/db6bb24439c6f7bfb818d1b3eb3eb2e1 to your computer and use it in GitHub Desktop.
changes bit order in strings representing binary numbers (e.g. 0b001 --> 0b100)
#!/usr/bin/python
bitstringlen = 8
import sys, re
regex = re.compile('(.*)0b([01]{2,8}),(.*)$');
for line in sys.stdin:
if regex.match(line) is not None:
bitstring = regex.match(line).group(2)
num = int (bitstring, 2);
revnum = int(('{:0%db}' % (len(bitstring))).format(num)[::-1], 2)
print ("%s0b%s,%s" % (regex.match(line).group(1), ('{:0%db}'%(bitstringlen)).format(revnum), regex.match(line).group(3)))
bitorderchange.py (END)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment