What is the difference between signed and unsigned variables?

Sam152 picture Sam152 · Mar 7, 2009 · Viewed 121.5k times · Source

I have seen these mentioned in the context of C and C++, but what is the difference between signed and unsigned variables?

Answer

coobird picture coobird · Mar 7, 2009

Signed variables, such as signed integers will allow you to represent numbers both in the positive and negative ranges.

Unsigned variables, such as unsigned integers, will only allow you to represent numbers in the positive and zero.

Unsigned and signed variables of the same type (such as int and byte) both have the same range (range of 65,536 and 256 numbers, respectively), but unsigned can represent a larger magnitude number than the corresponding signed variable.

For example, an unsigned byte can represent values from 0 to 255, while signed byte can represent -128 to 127.

Wikipedia page on Signed number representations explains the difference in the representation at the bit level, and the Integer (computer science) page provides a table of ranges for each signed/unsigned integer type.