Convert from int to bit string


/ Published in: Python
Save to your folder(s)

Couldn't find a built in way to do this. Neatly prints an int as bit string. Recursive so not the most efficient but switching it to iterative would be cake if performance is that big an issue. Warning doesn't support negative numbers... simple enough to convert to one or two's complement.


Copy this code and paste it in your HTML
  1. def bs(s):
  2. return str(s) if s<=1 else bs(s>>1) + str(s&1)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.