Forum Discussion
Altera_Forum
Honored Contributor
15 years agoOutput of "x^2 + y^2" from 2's complement to binary (with sign)
Hi, I am using a altmult_add megafunction to generate output of x^2 + y^2 for inputs x and y, where both x and y are 2's complements. I understand that the output would always be a positiv...
Altera_Forum
Honored Contributor
15 years agoNo, that is not the case.
when adding two N bit wide numbers the largest possible result is N+1 bit wide. let's assume both x² and y² are the largest possible 16 bit numbers (when using signed arithmetics): 0x7FFF and 0x7FFF or binary: 0111.1111.1111.1111 Adding those two numbers is equivalent to multiplying one of the numbers with 2. Multiplikation with 2 again is equivalent to shifting the number to the left by one, so we end up with: 0.1111.1111.1111.1110 which equals 0x0000FFFE When using unsigned arithmetics from now on we will not need the zeros at the beginning: so we end up with unsigned 0xFFFE.