Last active
November 25, 2016 20:09
-
-
Save Arcensoth/3213208b51bb7ec64a8228ae8060fd41 to your computer and use it in GitHub Desktop.
This file contains 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
while True: | |
try: | |
inp = input('>') # input flags in binary, like this: 01110010 | |
except KeyboardInterrupt: | |
print('\ngoodbye') | |
break | |
n = len(inp) # could also be locked (for example, at n=32 for integer capacity) | |
try: | |
flags = int(inp, 2) | |
except: | |
print('invalid input') | |
continue | |
print('you entered {inp} ({flags})'.format(inp=inp, flags=flags)) | |
for i in range(1, n + 1): | |
p = 2 ** i | |
q = 2 ** (i - 1) | |
m = flags % p | |
flag = m >= q | |
print('{i_str} {flag_str} p={p}, q={q}, m={flags}%{p}={m}, {m} {sign} {q}'.format( | |
i_str=str(i).ljust(4), flag_str=str(flag).ljust(8), flags=flags, p=p, q=q, m=m, sign='>=' if flag else '<')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credits to @Arth2000 for providing an explanation and for reducing the number of basic operations from 3 down to 2: