Question

I would like to read an 8 bit number that comes in a 2's complement fix8_7 (8 bit number and the binary point is in the 7 bit). How can I do this in Python?

Was it helpful?

Solution

I am assuming that you have (starting from the left end) one sign bit, the assumed binary point, and then seven bits that represent a fractional value. If that's the case then you can just take the signed integer value of the fixed-point number and divide by 128. You'll need to do this division using floating-point values, of course, because the result will be less than 1.

The range of values that can be represented in this fixed-point format is -1.0 to +(127/128).

OTHER TIPS

Assuming that your input is s = '1101001.1' (for example), you can use:

d = int(s[0:7],2)+int(s[8])/2.0

This will give an unsigned result of course. If you want to get a negative value for an input that starts with '1', then I guess that you can use d when s[0] == '0' and 64-d when s[0] == '1'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top