# Signed fixed number - 2s complement

Status
Not open for further replies.

#### nesta

##### Junior Member level 2 Hi VhdlExperts,

What is the algorithm to find a 2's complement for a signed fixed point number.

I would like to implement this so would need the algorithm.

eg: 2s complement of (-1.5).

#### TrickyDicky fixed point arithmatic is no different to normal arithmatic, so there is no need to "find" a value, and there is no algorithm for it. Its all about where you place the imaginary decimal place. To put a number into fixed point, you just scale it by 2^n.

So -1.5 = -3/2

-3 = 1101 in binary (4 bits)
-1.5 = 110.1 in binary (its just 3 bitshifted by 1 place).

So, if using the singed type, you still declare it with
signal my_number : signed(3 downto 0) := "1101";

But then you need to track the position of the decimal place.

What makes life easier is the new fixed point package in VHDL2008. You can now decalre signals like this:

signal my_number : sfixed(2 downto -1) := "1101";

So here, the first 3 bits (2 downto 0) make up the integer part, and the negative bits (-1) make up the fraction.

It makes tracking fixed point numbers a hell of a lot easier, and reads much better in the code.

• ravics

Points: 2