Skip to content

Instantly share code, notes, and snippets.

@onemouth
Created May 16, 2013 17:08
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 onemouth/5593339 to your computer and use it in GitHub Desktop.
Save onemouth/5593339 to your computer and use it in GitHub Desktop.
11 -> 10(-1)
trans2NegForm :: Integer -> [Int]
trans2NegForm x = loop x "init" []
where
trans 0 "init" = (0, "init")
trans 1 "init" = (-1, "trans")
trans 1 "trans" = (0, "trans")
trans 0 "trans" = (1, "init")
loop 0 "trans" ans = 1:ans
loop 0 "init" ans = ans
loop i status ans = loop (i `shiftR` 1) newStatus (newBit:ans)
where (newBit, newStatus) = trans (i .&. 1) status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment