1 year ago

#377811

test-img

Riaz

converting decimal to binary without the bin() function in python

I've looked into other solutions online and my own and all of them gave the exact same output, and thats my terminal being spammed with 1's.

this is my code, (I'm very new to coding so please forgive any clear cut mistakes :I)

def decimaltobinary(numb):
    bit = []
    x = numb // 2
    while numb > 1:
      y = x // 2
      bit.append(y)

this is the code I found online which gave the exact same output

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

python

binary

decimal

0 Answers

Your Answer

Accepted video resources