Skip to content

Instantly share code, notes, and snippets.

@PRotondo
Last active November 9, 2017 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PRotondo/e36af6724e2ab6c4f1aea0234649c8dd to your computer and use it in GitHub Desktop.
Save PRotondo/e36af6724e2ab6c4f1aea0234649c8dd to your computer and use it in GitHub Desktop.
Iterate over each element of a set in order. It is well-known that one can easily do it in reverse order by doing i = (i-1)&superset (see https://www.topcoder.com/community/data-science/data-science-tutorials/a-bit-of-fun-fun-with-bits/)
def binary(x) :
return format(x,'b')
def print_binary(x) :
print binary(x)
def iterate(superset,f) :
i = 0
while True :
f(i)
if i == superset :
break
i = ((i|(~superset))+1)&superset
iterate(53,print_binary)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment