Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created July 3, 2012 15:09
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 MartinThoma/3040329 to your computer and use it in GitHub Desktop.
Save MartinThoma/3040329 to your computer and use it in GitHub Desktop.
Takes a number and a number of bits and returns a True-False representation of the number with bits bits.
#!/usr/bin/python
# -*- coding: utf-8 -*-
def getBinary(number, bits):
""" Takes a number and a number of bits and returns a True-False
representation of the number with bits bits.
@param number: The number you want to convert
@param bits: The number of bits
@return a list of True / False values
"""
returnVal = []
places = bits-1
while places >= 0:
if number >= 2**places:
number -= 2**places
returnVal.append(True)
else:
returnVal.append(False)
places -= 1
return returnVal
print getBinary(3, 4)
print getBinary(5, 4)
print getBinary(12, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment