Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent a negative number with a fraction in 2's complement?

Tags:

So I want to represent the number -12.5. So 12.5 equals to:

001100.100 

If I don't calculate the fraction then it's simple, -12 is:

110100 

But what is -12.5? is it 110100.100? How can I calculate this negative fraction?

like image 586
Tom Avatar asked Mar 17 '13 16:03

Tom


People also ask

How do you represent a negative number in two's complement?

Negating a two's complement number is simple: Invert all the bits and add one to the result. For example, negating 1111, we get 0000 + 1 = 1. Therefore, 1111 in binary must represent −1 in decimal.

How do you convert a negative fraction to binary?

To convert negative, procedure is standard, invert all bits and add 1 to the least significand digit. That's all.


1 Answers

With decimal number systems, each number position (or column) represents (reading a number from right to left): units (which is 10^0), tens (i.e. 10^1),hundreds (i.e. 10^2), etc.

With unsigned binary numbers, the base is 2, thus each position becomes (again, reading from right to left): 1 (i.e. 2^0) ,2 (i.e. 2^1), 4 (i.e. 2^2), etc.

For example

2^2 (4), 2^1 (2), 2^0 (1). 

In signed twos-complement the most significant bit (MSB) becomes negative. Therefore it represent the number sign: '1' for a negative number and '0' for a positive number.

For a three bit number the rows would hold these values:

-4, 2, 1  0  0  1 => 1  1  0  0 => -4  1  0  1 => -4 + 1 = -3 

The value of the bits held by a fixed-point (fractional) system is unchanged. Column values follow the same pattern as before, base (2) to a power, but with power going negative:

2^2 (4), 2^1 (2), 2^0 (1) . 2^-1 (0.5), 2^-2 (0.25), 2^-3 (0.125) 

-1 will always be 111.000
-0.5 add 0.5 to it: 111.100

In your case 110100.10 is equal to -32+16+4+0.5 = -11.5. What you did was create -12 then add 0.5 rather than subtract 0.5.

What you actually want is -32+16+2+1+0.5 = -12.5 = 110011.1

like image 138
Morgan Avatar answered Oct 20 '22 21:10

Morgan