decimal_binary.py

Created by uncurieux

Created on May 30, 2019

304 Bytes

bitodec convertit du binaire au décimal, et dectobi du décimal au binaire.


def bitodec(a):
  b,c=str(a),0
  for i in range(len(b)):
    c+=int(b[i])*2**(len(b)-i)
  return c//2

def dectobi(a):
  t,b="1",0
  while 2**(b+1)<=a:
    b+=1
  a-=2**b
  for i in range(b-1,-1,-1):
    if a-2**i<0:
      t+="0"
    else:
      t+="1"
      a-=2**i
  return int(t)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.