# Signedness

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Signedness
> Markdown URL: https://mediated.wiki/source/Signedness.md
> Source: https://en.wikipedia.org/wiki/Signedness
> Source revision: 1319180168
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Property of a numeric data type in computing}}
In computing, '''signedness''' is a property of [data type](/source/data_type)s representing [number](/source/number)s in computer programs. A numeric variable is ''signed'' if it can represent both [positive](/source/positive_number) and [negative](/source/negative_number) numbers, and ''unsigned'' if it can only represent [non-negative](/source/non-negative) numbers (zero or positive numbers).

As [''signed''](/source/Sign_(mathematics)) 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](/source/value_(programming)) are non-positive values, whereas the respective unsigned type can dedicate all the possible values to the positive number range.

For example, a [two's complement](/source/two's_complement) signed 16-bit [integer](/source/integer_(computer_science)) can hold the values −32768 to 32767 inclusively, while an unsigned 16 bit integer can hold the values 0 to [65535](/source/65535_(number)). For this [sign representation](/source/Signed_number_representations) method, the leftmost bit ([most significant bit](/source/most_significant_bit)) denotes whether the value is negative (0 for positive or zero, 1 for negative).

== In programming languages ==
For most architectures, there is no signed–unsigned type distinction in the [machine language](/source/machine_language). Nevertheless, [arithmetic](/source/computer_arithmetic) instructions usually set different [CPU flags](/source/status_register) such as the [carry flag](/source/carry_flag) for unsigned arithmetic and the [overflow flag](/source/overflow_flag) for signed. Those values can be taken into account by subsequent [branch](/source/branch_instruction) or arithmetic commands.

The [C programming language](/source/C_programming_language), along with its derivatives (except [Java](/source/Java_(programming_language)) which lacks unsigned types), implements a signedness for all [integer data types](/source/integer_(computing)), as well as for ["character"](/source/character_(computing)). For Integers, the {{code|unsigned}} modifier defines the type to be unsigned. The default integer signedness outside bit-fields is signed, but can be set explicitly with {{code|signed}} modifier. By contrast, the C standard declares {{code|signed char}}, {{code|unsigned char}}, and {{code|char}}, to be ''three'' distinct types, but specifies that all three must have the same size and alignment.  Further, {{code|char}} must have the same numeric range as either {{code|signed char}} or {{code|unsigned char}}, but the choice of which depends on the platform.  Integer [literals](/source/literal_(computer_programming)) can be made unsigned with {{mono|U}} suffix.

Compilers often issue a warning when comparisons are made between signed and unsigned numbers or when one is [cast](/source/type_conversion) to the other. These are potentially dangerous operations as the ranges of the signed and unsigned types are different.

{| class="wikitable" style="text-align: right;"
|+ Data types
|-
! Bits !! Min !! Max
|-
| 8 (signed) || −128 || 127
|-
| 16 (signed) || {{val|−32768}} || {{val|32767}}
|-
| 32 (signed) || {{val|−2147483648}} || {{val|2147483647}}
|-
| 64 (signed) || {{val|−9223372036854775808}} || {{val|9223372036854775807}}
|-
| 128 (signed) || {{val|−170141183460469231731687303715884105728}} || {{val|170141183460469231731687303715884105727}}
|-
| 8 (unsigned) || 0 || 255
|-
| 16 (unsigned) || 0 || {{val|65535}}
|-
| 32 (unsigned) || 0 || {{val|4294967295}}
|-
| 64 (unsigned) || 0 || {{val|18446744073709551615}}
|-
| 128 (unsigned) || 0 || {{val|340282366920938463463374607431768211455}}
|}

==See also==
*[Sign bit](/source/Sign_bit)
*[Signed number representations](/source/Signed_number_representations)
*[Sign (mathematics)](/source/Sign_(mathematics))
* [Binary Angular Measurement System](/source/Binary_Angular_Measurement_System), an example of semantics where signedness does not matter

== External links ==
* {{cite web |url=http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html |title=Numeric Type Overview |year=2011 |work=[MySQL](/source/MySQL) 5.0 Reference Manual |publisher=mysql.com |access-date=6 January 2012}}
* {{Citation
| url   = https://www.securecoding.cert.org/confluence/display/c/INT02-C.+Understand+integer+conversion+rules
| title = Understand integer conversion rules
| work  = CERT C Coding Standard
| publisher  = [Computer emergency response team](/source/Computer_emergency_response_team)
| access-date = December 31, 2015
| ref = none
}}

{{Data types}}

Category:Computer arithmetic
Category:Data types
Category:Sign (mathematics)

---
Adapted from the Wikipedia article [Signedness](https://en.wikipedia.org/wiki/Signedness) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Signedness?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
