In computing, signedness is a property of
data type
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
s representing
number
A number is a mathematical object used to count, measure, and label. The most basic examples are the natural numbers 1, 2, 3, 4, and so forth. Numbers can be represented in language with number words. More universally, individual numbers can ...
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
In mathematics, the sign of a real number is its property of being either positive, negative, or 0. Depending on local conventions, zero may be considered as having its own unique sign, having no sign, or having both positive and negative sign. ...
numbers (zero or positive numbers).
As
''signed'' 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
In ethics and social sciences, value denotes the degree of importance of some thing or action, with the aim of determining which actions are best to do or what way is best to live ( normative ethics), or to describe the significance of different a ...
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
Two's complement is the most common method of representing signed (positive, negative, and zero) integers on computers, and more generally, fixed point binary values. Two's complement uses the binary digit with the ''greatest'' value as the ''s ...
signed 16-bit
integer
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
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 method, the leftmost bit (
most significant bit
In computing, bit numbering is the convention used to identify the bit positions in a binary numeral system, binary number.
Bit significance and indexing
In computing, the least significant bit (LSb) is the bit position in a Binary numeral sy ...
) 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
In computer programming, machine code is computer code consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). For conventional binary computers, machine code is the binaryOn nonb ...
. Nevertheless,
arithmetic
Arithmetic is an elementary branch of mathematics that deals with numerical operations like addition, subtraction, multiplication, and division. In a wider sense, it also includes exponentiation, extraction of roots, and taking logarithms.
...
instructions usually set different
CPU flags such as the
carry flag
In computer processors, the carry flag (usually indicated as the C flag) is a single bit in a system status register
A status register, flag register, or condition code register (CCR) is a collection of status Flag (computing), flag bits for a ...
for unsigned arithmetic and the
overflow flag
In computer processors, the overflow flag (sometimes called the V 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 ...
for signed. Those values can be taken into account by subsequent
branch
A branch, also called a ramus in botany, is a stem that grows off from another stem, or when structures like veins in leaves are divided into smaller veins.
History and etymology
In Old English, there are numerous words for branch, includ ...
or arithmetic commands.
The
C programming language
C (''pronounced'' '' – like the letter c'') is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
, along with its derivatives, implements a signedness for all
integer data types, as well as for
"character". For Integers, the modifier defines the type to be unsigned. The default integer signedness outside bit-fields is signed, but can be set explicitly with modifier. By contrast, the C standard declares , , and , to be ''three'' distinct types, but specifies that all three must have the same size and alignment. Further, must have the same numeric range as either or , but the choice of which depends on the platform. Integer
literals can be made unsigned with suffix.
Compilers often issue a warning when comparisons are made between signed and unsigned numbers or when one is
cast
Cast may refer to:
Music
* Cast (band), an English alternative rock band
* Cast (Mexican band), a progressive Mexican rock band
* The Cast, a Scottish musical duo: Mairi Campbell and Dave Francis
* ''Cast'', a 2012 album by Trespassers William ...
to the other. These are potentially dangerous operations as the ranges of the signed and unsigned types are different.
See also
*
Sign bit
*
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 minus sign ("−"). However, in RAM or CPU reg ...
*
Sign (mathematics)
In mathematics, the sign of a real number is its property of being either positive, negative, or 0. Depending on local conventions, zero may be considered as having its own unique sign, having no sign, or having both positive and negative sign. ...
*
Binary Angular Measurement System, an example of semantics where signedness does not matter
External links
*
*
{{Data types
Computer arithmetic
Data types
Sign (mathematics)