Convert an integer to binary without using the built-in bin function

后端 未结 15 1521
深忆病人
深忆病人 2020-12-07 01:14

This function receives as a parameter an integer and should return a list representing the same value expressed in binary as a list of bits, where the first element in the l

15条回答
  •  再見小時候
    2020-12-07 01:16

    def trans(x):
        if x == 0: return [0]
        bit = []
        while x:
            bit.append(x % 2)
            x >>= 1
        return bit[::-1]
    

提交回复
热议问题