C# Short Error: Negating the minimum value of a twos complement number is invalid

user488792 picture user488792 · Jun 7, 2011 · Viewed 13.1k times · Source

I have been encountering this error for my project, which involves working with Digital Audio Signals.

So I have been getting the amplitude values and recently encountered this error. This occurs when the amplitude value encountered is "-32768" upon debugging. I am storing the values in a short[] array.

I have a hunch that it has something to do with max/minimum values (I use Math.Abs) but I am unsure on how to handle it.

Can someone help? Thanks!

Answer

David Heffernan picture David Heffernan · Jun 7, 2011

16 bit signed int (short) takes values between -32,768 and 32,767.

Negating -32768, or getting the absolute value, is impossible to do inside a 16 bit signed integer. The value (32,768) is greater than the maximum possible positive value (32,767).

I would not like to advise you how to solve the problem without knowing more details of the algorithms you are using.