4

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?

Stephen Diehl
  • 8,271
  • 5
  • 38
  • 56
maverick88
  • 41
  • 1
  • 7

2 Answers2

3

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).

0

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'.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • I suggest you read the question again. The OP said the input is an 8 bit 2's complement number with an assumed binary point, not a string. They also mention it again in a comment. – martineau Feb 27 '16 at 00:14
  • 1
    @martineau: I suggest you check the exact time at which I posted this answer in opposed to the exact time at which OP has posted the comment, before you down-vote me for it!!!!! – barak manos Feb 27 '16 at 08:07
  • I based my down-vote on the OP's question and your answer which are within minutes of one another -- it was only afterwards, while writing the accompanying comment, that I also noticed that the OP had left a comment pointing out exactly the same discrepancy. – martineau Feb 27 '16 at 09:10
  • @martineau: Well, as you can see, my answer starts with "assuming that your input is a" (string), which was obviously not really clear in OP's question to begin with (the first comment to the question even mentions it). If my answer was wrong under that context (of the input being a string), then I would understand and even appreciate a comment explaining it. Other than that, I think your reasoning for down-voting it is nothing more than nit picking!!! – barak manos Feb 27 '16 at 10:16
  • Sorry, answers that say, well, if you asked a different question, the solution would be ...whatever.., don't desire any up-votes they may have gotten IMO. My vote was to cancel out the one you had at that point and wasting everyone's time...otherwise I would have simply ignored it, – martineau Feb 27 '16 at 11:18
  • @martineau: 1. It doesn't waste anyone's time. If anything, it helps those who by chance are looking for a solution given this type of input (which, as I've already mentioned, is not obvious from the question). 2. This question is two years old, and you're scanning SO for (what you think are) "undesirable up-votes"??? Here's my suggestion then - go get a life!!! – barak manos Feb 27 '16 at 13:48