请输入您要查询的百科知识:

 

词条 Signedness
释义

  1. In programming languages

  2. See also

  3. External links

{{refimprove|date=December 2009}}

In computing, signedness is a property of data types representing numbers 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 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 half the possible values are non-positive values (so if an 8-bit is signed, positive unsigned values 128 to 255 are gone while -128 to 127 are present). Unsigned variables can dedicate all the possible values to the positive number range.

For example, a two's complement 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 method, the leftmost bit (most significant bit) 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 such as the carry flag for unsigned arithmetic and the overflow flag for signed. Those values can be taken into account by subsequent branch or arithmetic commands.

The C programming language, along with its derivatives, implements a signedness for all integer data types, as well as for "character". The unsigned modifier defines the type to be unsigned. The default integer signedness is signed, but can be set explicitly with signed modifier. Integer literals can be made unsigned with U suffix. For example, 0xFFFFFFFF gives −1, but 0xFFFFFFFFU 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 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
  • Sign (mathematics)
  • 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 5.0 Reference Manual |publisher=mysql.com |accessdate=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
| accessdate = December 31, 2015
| ref = none
}}{{Data types}}

2 : Computer arithmetic|Data types

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/13 7:59:49