Search code examples
binarynumbersdecimalconvertersnumber-formatting

Converting a two's complement in fix-point arithmetic (with bit numbering) to decimal number


I'm wondering how you could convert a two's complement in fix-point arithmetic to a decimal number.

So let's say we got this fix-point arithmetic in two's complement: 11001011 with bit numbering, with 2 positions behind the decimal point, and want form it to a decimal number.

We already know that the decimal will be negative because the first bit is a 1.

2 positions behind decimal point, so we have 110010 11.

Convert that from two's complement to normal form (sub by 1, invert):

110010 10 (i sub by 1 here)

001101 01 (i inverted here)

001101 in decimal is 13

01 in decimal is 1

So in the end we get to -13.1. Is that correct or there isn't even a way to convert this?


Solution

  • The simplest method is just to convert the whole value to an integer (ignoring the fixed point, initially), then scale the result.

    So for your example where you have a 6.2 fixed point number: 110010 10:

    Convert as integer:

    11001010 = -54
    

    Divide by scale factor = 2^2:

    -54 / 4 = -13.5
    

    Note that the fractional part is always unsigned. (You can probably see now that 10 would give you + 0.5 for the fractional part, i.e. 00 = 0.0, 01 = +0.25, 10 = +0.5, 11 = +0.75.)