Created
July 3, 2012 15:09
-
-
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.
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
#!/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