Created
October 12, 2016 17:39
-
-
Save speters/db6bb24439c6f7bfb818d1b3eb3eb2e1 to your computer and use it in GitHub Desktop.
changes bit order in strings representing binary numbers (e.g. 0b001 --> 0b100)
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
| #!/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