0

Is there a way to obtain the binary representation of a number in Python? It has to be able to represent negative numbers as well. I already tried the function from numpy np.binary_repr(Decimal(-1)) but it results in an error. To be precise - I want to somehow obtain the binary representations of fixed point numbers (positive and negative), so I can put them into Verilog code.

Let's say I have the number 1,5 in decimal. I want to write it with two bit precision and three integer bits and a sign. In binary this would be 0001,10, so basically 000110. I could do this easily by hand, but the problem is that I also have negative ones and furthermore they don't have a finite expansion after the point (like 1/3 in decimal)

To be more precise: If I have a number like -1.5 in decimal a sign, fraction precision of two, and integer precision of two, I expect the number 110,10 to be the output - the first digit is the sign, the next two the number, the next two are the fraction.

Adrian Jałoszewski
  • 1,695
  • 3
  • 17
  • 33
  • interesting, but can you show us input & expected ouput ? what does Verilog has to do with python internal number representation? I think you want to "emulate" Verilog code expected number representation instead. – Jean-François Fabre Apr 05 '17 at 09:39
  • It's not about pythons internal number representation. Let's say I have the number `1,5` in decimal. I want to write it with two bit precision and three integer bits and a sign. In binary this would be `0001,10`, so basically `000110`. I could do this easily by hand, but the problem is that I also have negative ones and furthermore they don't have a finite expansion after the point (like `1/3` in decimal). – Adrian Jałoszewski Apr 05 '17 at 09:45
  • Possible duplicate of [Float to binary](http://stackoverflow.com/questions/4838994/float-to-binary) – Hans Apr 05 '17 at 09:50
  • @kamik423 Nope it's fixed point here and not floating point. – Adrian Jałoszewski Apr 05 '17 at 09:55

4 Answers4

2

bin(number) might be the function you are looking for. It supports negative numbers, but has a somewhat peculiar format

>>> bin(4)
'0b100'
>>> bin(-3)
'-0b11'
>>> bin(10000000)
'0b100110001001011010000000'

You could build something like this to convert it to a more common notation:

def binary(number):
    b = bin(number)
    if b[0] == '-':
        return '-' + b[3:]
    else:
        return b[2:]
Hans
  • 2,354
  • 3
  • 25
  • 35
1

You can create fixed-point number throught various fixed-point arithmetic library like numfi

>>> from numfi import numfi  
>>> x = numfi(-1.5,1,5,2) 
>>> x.bin  
array(['11010'], dtype='<U5')
>>> x.bin_
array(['110.10'], dtype='<U6')

But if all you need is transforming single decimal number to fixed length binary representation and no arithmetic operation, you can just use np.binary_repr(x,width)

np.binary_repr(np.round(num*(2**f)).astype(int), width=w) 
# num=-1.5, f=2, w=5 (2 fraction, 2 integer, 1 signed) for your example

you can use np.floor/np.ceil/.astype(int) to replace np.round, for different rounding method

0

I recommend fxpmath module for that.

The repo website is: https://github.com/francof2a/fxpmath

from fxpmath import Fxp

x = Fxp(-1.5, True, 5, 2)   # (val, signed, n_word, n_frac)

print(x.bin())

Result:

11010
francof2a
  • 154
  • 4
0

You can use the Binary fractions package. With this package you can convert binary-fraction strings into floats and vice-versa.

Example:

>>> from binary_fractions import Binary
>>> str(Binary(-1.5))
'-0b1.1'
>>> float(Binary("-1.1"))
-1.5

It has many more helper functions to manipulate binary strings such as: shift, add, fill, to_exponential, invert...

PS: Shameless plug, I'm the author of this package.