lithium_js
lithium_js

Reputation: 151

Python: binary digits

I have a two-digit number (42 for example) and i have to get a list of bits for every digit like this

[[0, 1, 0, 0], [0, 0, 1, 0]]

how to do that?

Upvotes: 0

Views: 31

Answers (1)

ResVS
ResVS

Reputation: 21

def bin(s):
   return str(s) if s<=1 else bin(s>>1) + str(s&1)

it's function for one digit, if you have multiple digits, do it for x % 10 and then divide your number by 10 for every digit

Upvotes: 2

Related Questions