Python实现的十进制小数与二进制小数相互转换功能

Python实现的十进制小数与二进制小数相互转换功能
link

def dec2bin(x):
  x -= int(x)
  bins = []
  while x:
    x *= 2
    bins.append(1 if x>=1. else 0)
    x -= int(x)
  return bins
print(dec2bin(.8125))
      # [1, 1, 0, 1]
posted @ 2022-08-19 22:43  luoganttcc  阅读(17)  评论(0编辑  收藏  举报