1
votes

ello guys,

I know this is really silly but I am a little confused about how to do the following operation by hand:

0.25-0.5? or any similar questions

Also, when it comes to fixed point signed numbers, how do you subtract them?

Say I choose my MSB to be the sign and the 7th bit in an 8 bit number to be 1, while bits 6-0 are the fractional part as shown below:

[SIGN][2^0][2^-1][2^-2][2^-3][2^-4][2^-5][2^-6]

So for example: 01.100000 is 1.5 in decimal.

When aligning the decimal point its easy to add say 1+0.25:

01.000000 +00.010000 =01.010000 = 1.25 in decimal

But how would I do 0.25-1? or -0.25-1?

Hope you can help!

1
why do you ask 2 questions about fixed-point arithmetics? Why don't just combine it in 1?phuclv

1 Answers

3
votes

Using a sign-magnitude representation, like you've done here, will complicate matters greatly (and will saddle you with a bit pattern which represents "negative zero", separate from the all-zero "normal zero"). It's certainly a workable representation, but, as you're discovering, it's considerably more complicated to work with.

Using a two's complement representation, where the highest bit represents a negative value of twice the magnitude of the next highest bit (in this case, -2^1) will simplify matters immensely, as it will allow you to perform addition and subtraction exactly as if both numbers were unsigned. With such a representation, for instance:

 0.25 = 00 010000
-1.00 = 11 000000
      -----------
        11 010000 = -0.75

and

-0.25 = 11 110000
-1.00 = 11 000000
      -----------
        10 110000 = -1.25

To convert from the sign-magnitude representation to two's complement:

  • If the number is positive, leave it alone.
  • If the number is negative, invert every bit other than the sign bit and add 1 (using unsigned addition).

For instance, to convert from your sign-magnitude representation of -0.75 to two's complement:

Sign/magnitude: 10 110000
        Invert: 11 001111
            +1: 11 010000 -> two's complement

To convert in the opposite direction, reverse the process (subtract 1, then invert).