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 ...
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 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 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
65535 is the integer after 65534 and before 65536.
It is the maximum value of an unsigned 16-bit integer.
In mathematics
65535 is the product of the first four Fermat primes: 65535 = (2 + 1)(4 + 1)(16 + 1)(256 + 1). Because of this property ...
. For this
sign representation method, the leftmost bit (
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. 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 c ...
instructions usually set different
CPU flags such as the
carry flag for unsigned arithmetic and the
overflow flag 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 of a tree (or sometimes a shrub). Large branches are known as boughs and small branches are known as twigs. The term '' twig'' usually ...
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 te ...
*
Signed number representations
*
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
Binary may refer to:
Science and technology Mathematics
* Binary number, a representation of numbers using only two digits (0 and 1)
* Binary function, a function that takes two arguments
* Binary operation, a mathematical operation that ta ...
, an example of semantics where signedness does not matter
External links
*
*
{{Data types
Computer arithmetic
Data types