I want to convert number -0.25. So 0.25 (for 4 bit int and 4 bit frac) equals to:
0000_0100
For negative number -1 answer is
1111
But what is -0.25? How can I convert this negative number with a fraction in 2's complement?
I want to convert number -0.25. So 0.25 (for 4 bit int and 4 bit frac) equals to:
0000_0100
For negative number -1 answer is
1111
But what is -0.25? How can I convert this negative number with a fraction in 2's complement?
To answer this question, I need to clarify that the numbers are represented in a fixed-point. Almost all logic nowadays uses two's complement. Therefore, in the two's complement version negative numbers are considered for example:
signed 4'b1100 = -4 (-1*2^3 + 1*2^2 + 0*2^1 + 0*2^0).
Number for example 0.25 in fixed-point binary will be represented:
8'b0000_0100 (int 4 bit is zeros and frac 4 bit -> 0*2^-1 + 1*2^-2 + 0*2^-3 + 0*2^-4).
Other way to calculate can be multiply the number to integer find negative and then divide this number (remember, that multiplication on 2 is shift to the right, and division on 2 is shift to the left).
For my example -0.25 would be as follows:
-0.25*2*2= -1.
-1 = 4'b1111.
shift 2*2 to left = 4'b11_11 = -0.25.
Or we can calculate: -1*2^1 + 1*2^0 + 0*2^-1 + 1*2^-2.
1 in MSB is signed bit. We must remember this when operate with signed logic.