How to convert between floats and decimal twos complement numbers in Python? -


i want convert decimal float numbers in python twos complement decimal binary numbers. example 1.5 in twos complement decimal 2.6 (8 bits) 0b011000.

is there module can me?

what you're describing has nothing @ decimal. want convert float fixed-point binary representation. this, multiply float scale factor, cast integer, , use python's built-in string formatting tools string representation:

def float_to_binary(float_):     # turns provided floating-point number fixed-point     # binary representation 2 bits integer component ,     # 6 bits fractional component.      temp = float_ * 2**6  # scale number up.     temp = int(temp)     # turn integer.     # if want -1 display 0b11000000, include part:     # if temp < 0:     #     temp += 2**8      # 0 means "pad number zeros".     # 8 means pad width of 8 characters.     # b means use binary.     return '{:08b}'.format(temp) 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -