-3

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?

Nivensy
  • 1
  • 3
  • 1
    You are talking about so called fixed-point numbers. They are identical in all ways to standard integer (or non-fractional binary) number. So they are the same positive as negative. The fractional part does not really exist other then by definition. – Oldfart Oct 10 '19 at 14:59
  • OK. And answer for my example is: 8'b1111_0111? Exactly? – Nivensy Oct 10 '19 at 15:20
  • No, that is the one's complement. Almost all logic nowadays uses two's complement. I suggest you get yourself first acquainted with normal binary numbers, positive, negative, one's complement, two's complement etc. before you try anything with fractions. – Oldfart Oct 10 '19 at 15:28
  • you need to do your homework and find the information needed in your class, over internet, or in a library. The forum does not provide such information. – Serge Oct 10 '19 at 17:59
  • @Serge, I do not study. I learn Verilog of interest. And faced with the problem of lack of knowledge. However, I already found the answer to my question here on the site. Someone had asked the exact same question before. – Nivensy Oct 11 '19 at 09:30

1 Answers1

0

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.

Nivensy
  • 1
  • 3