Signedness
Encyclopedia
In computing, signedness is a property of data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

s representing number
Number
A number is a mathematical object used to count and measure. In mathematics, the definition of number has been extended over the years to include such numbers as zero, negative numbers, rational numbers, irrational numbers, and complex numbers....

s in computer programs. A numeric variable is signed if it can represent both positive and negative numbers, and unsigned if it can only represent non-negative numbers (zero or positive numbers).

As signed
Sign (mathematics)
In mathematics, the word sign refers to the property of being positive or negative. Every nonzero real number is either positive or negative, and therefore has a sign. Zero itself is signless, although in some contexts it makes sense to consider a signed zero...

 numbers can represent negative numbers, they lose a range of positive numbers that can only be represented with unsigned numbers of the same size (in bits) because roughly half the possible values are non-positive values. Unsigned variables can dedicate all the possible values to the positive number range.

For example, a Two's complement
Two's complement
The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two...

 signed 16 bit integer can hold the values −32768 to 32767 inclusively, while an unsigned 16 bit integer can hold the values 0 to 65535. For this sign representation
Signed number representations
In computing, signed number representations are required to encode negative numbers in binary number systems.In mathematics, negative numbers in any base are represented by prefixing them with a − sign. However, in computer hardware, numbers are represented in binary only without extra...

 method, the leftmost bit (most significant bit
Most significant bit
In computing, the most significant bit is the bit position in a binary number having the greatest value...

) denotes whether the value is positive or negative (0 for positive, 1 for negative),

In programming languages

For most architectures, there is no signed–unsigned type distinction in the machine language. Nevertheless, arithmetic instructions usually set different CPU flags
Status register
A status register or flag register is a collection of flag bits for a processor. An example is the FLAGS register of the x86 architecture....

 such as the carry flag
Carry flag
In computer processors the carry flag is a single bit in a system status register used to indicate when an arithmetic carry or borrow has been generated out of the most significant ALU bit position...

 for unsigned arithmetic and the overflow flag
Overflow flag
In computer processors, the overflow flag is usually a single bit in a system status register used to indicate when an arithmetic overflow has occurred in an operation, indicating that the signed two's-complement result would not fit in the number of bits used for the operation...

 useful for signed one, those values can be taken into account by subsequent branch or arithmetic commands.

The C programming language, with its derivatives, implements the signedness for all integer data types, as well as for "character"
Character (computing)
In computer and machine-based telecommunications terminology, a character is a unit of information that roughly corresponds to a grapheme, grapheme-like unit, or symbol, such as in an alphabet or syllabary in the written form of a natural language....

. The unsigned modifier defines the type to be unsigned. The default integer signedness is signed, but could be set explicitly with signed modifier. Integer literals can be made unsigned with U suffix: for example, 0x
Hexadecimal
In mathematics and computer science, hexadecimal is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F to represent values ten to fifteen...

FFFFFFFF gives −1, but 0xFFFFFFFFU gives 4,294,967,295 respectively for a 32-bit code.

However, an issue that will occasionally occur when using both signed and unsigned numbers is called a signed/unsigned mismatch. Examples of this would be when comparing Number 1 and Number 2, or assigning the value of Number 1 to Number 2, when Number 1 is signed and Number 2 is unsigned (or vice versa).

Compilers will usually output a Warning, but should continue to compile the code anyway, such as with Visual Studio (2010 in this example):

Warning 25 warning C4018: '<=' : signed/unsigned mismatch [C:/Some file.cpp] 23417

(A signed/unsigned mismatch).

An issue with signed and unsigned numbers is also when attempting to cast
Type conversion
In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations...

 one number into another with different signedness as this can potentially cause data corruption
Data corruption
Data corruption refers to errors in computer data that occur during writing, reading, storage, transmission, or processing, which introduce unintended changes to the original data...

 due to the range of numbers for a signed integer differing to that of an unsigned integer.

See also

  • Sign bit
    Sign bit
    In computer science, the sign bit is a bit in a computer numbering format that indicates the sign of a number. In IEEE format, the sign bit is the leftmost bit...

  • Signed number representations
    Signed number representations
    In computing, signed number representations are required to encode negative numbers in binary number systems.In mathematics, negative numbers in any base are represented by prefixing them with a − sign. However, in computer hardware, numbers are represented in binary only without extra...

  • Sign (mathematics)
    Sign (mathematics)
    In mathematics, the word sign refers to the property of being positive or negative. Every nonzero real number is either positive or negative, and therefore has a sign. Zero itself is signless, although in some contexts it makes sense to consider a signed zero...

  • Binary Angular Measurement System, an example of semantics where signedness does not matter
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK