In computing, signedness is a property of
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
s representing
number
A number is a mathematical object used to count, measure, and label. The original 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 c ...
s in computer programs. A numeric variable is ''signed'' if it can represent both
positive
Positive is a property of positivity and may refer to:
Mathematics and science
* Positive formula, a logical formula not containing negation
* Positive number, a number that is greater than 0
* Plus sign, the sign "+" used to indicate a posi ...
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 zero. Depending on local conventions, zero may be considered as being neither positive nor negative (having no sign or a unique third sign), or it ...
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 something or action, with the aim of determining which actions are best to do or what way is best to live (normative ethics in ethics), or to describe the significance of dif ...
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 a mathematical operation to reversibly convert a positive binary number into a negative binary number with equivalent (but negative) value, using the binary digit with the greatest place value (the leftmost bit in big- endian ...
signed 16-bit
integer
An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
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 number.
Bit significance and indexing
In computing, the least significant bit (LSB) is the bit position in a binary integer representing the binar ...
) 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 any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a very ...
. Nevertheless,
arithmetic
Arithmetic () is an elementary part of mathematics that consists of the study of the properties of the traditional operations on numbers— addition, subtraction, multiplication, division, exponentiation, and extraction of roots. In the 19th ...
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/flag register used to indicate when an arithmetic carry or borrow has been generated out of the most significant arithmetic l ...
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 r ...
for signed. Those values can be taken into account by subsequent
branch
A branch, sometimes called a ramus in botany, is a woody structural member connected to the central trunk (botany), trunk of a tree (or sometimes a shrub). Large branches are known as boughs and small branches are known as twigs. The term '' ...
or arithmetic commands.
The
C programming language
''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
, 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 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. For example, gives −1, but gives 4,294,967,295 for 32-bit code.
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
In computer science, the sign bit is a bit in a signed number representation that indicates the sign of a number. Although only signed numeric data types have a sign bit, it is invariably located in the most significant bit position, so the term ...
*
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 regis ...
*
Sign (mathematics)
In mathematics, the sign of a real number is its property of being either positive, negative, or zero. Depending on local conventions, zero may be considered as being neither positive nor negative (having no sign or a unique third sign), or it ...
*
Binary Angular Measurement System, an example of semantics where signedness does not matter
External links
*
*
{{Data types
Computer arithmetic
Data types