Signed number and unsigned number

Source: Internet
Author: User

Excerpt from: http://www.cnblogs.com/glacierh/archive/2013/07/16/3194658.html

1. Complement

The unsigned number in the computer is represented by the original code, and the signed number is expressed in complement. The value represented by W-complement is:

The highest bit is also called the sign bit, 1 is negative, 0 is positive, the sign bit is 0 o'clock, and the representation of the unsigned number is the same, and the following is an example of a 4-bit complement:

0101 = -0*23 + 1*22 + 0*21 + 1*20 = 5

1101 = -1*23 + 1*22 + 0*21 + 1*20 = 3

The value range of the W-bit complement representation is [ -2w-1, 2w-1-1], such as the 4-bit complement representation of the minimum value is-8 (1000), the maximum value is 7 (0111).

Only by understanding the complement representation of signed numbers can we really understand the problems of unsigned number and signed number conversion, the truncation and overflow of signed number.

2. Conversion of unsigned and signed numbers

The coercion type conversion in C language keeps the bits value unchanged, just changing the way the bit is interpreted. Look at the following code:

Short int v =-12345;

unsigned short UV = (unsigned short) v;

printf ("V =%d, UV =%u\n", V, UV);

The output is as follows:

v = -12345, UV = 53191

Since the 12345 16-bit complement representation is exactly the same as the 53191 16-bit unsigned representation, the above output will be obtained.

The conversion between the unsigned number and the signed number is a one by one corresponding relationship, and the corresponding relationship of the signed number s of the W bit to the unsigned number U is:

If the 4-bit signed number 7 (0111) is converted to the unsigned number is also 7, and the 4-bit signed number-1 (1111) is converted to an unsigned number of 15.

Similarly, the unsigned number U of W-bit is converted to the corresponding relationship of the signed number s:

If the 4-bit unsigned number 5 (0101) is converted to the unsigned number is also 5, and the 4-bit unsigned number 13 (1101) is converted to the unsigned number-3.

In fact, as long as the unsigned number and signed number of bits interpretation of the way, do not have to remember the corresponding relationship, but also to calculate the converted value.

3. Traps

In C, if an operation contains a signed number and an unsigned number, then the C language implicitly converts the signed number to an unsigned number, which is fine for standard arithmetic operations, but for relational operators such as < and >, it can have non-visual results. This non-intuitive feature often leads to difficult-to-detect errors in the program. Look at the following example:

int Strlonger (char *s, char *t)

{

return strlen (s)-strlen (T) > 0;

}

The above function seems to have no problem, in fact, when s is shorter than T, the return value of the function is also 1, why does this happen? The original strlen return value type defines size_t as unsigned int in the Size_t,c language, and when s is shorter than T, strlen (s)-strlen (t) is negative, but the result of an unsigned number of operations implicitly converts to an unsigned number becomes a large unsigned number. In order for the function to work correctly, the code should be modified as follows:

return strlen (s) > strlen (t);

In 2002, programmers working on the FreeBSD open source operating system realized that there was a security vulnerability to the implementation of the Getpeername function. The simplified version of the code is as follows:

void *memcpy (void *dest, void *src, size_t n);

#define KSIZE 1024

Char Kbuf[ksize];

int Copy_from_kernel (void *user_dest, int maxlen)

{

int len = Ksize < MaxLen? Ksize:maxlen;

memcpy (User_dest, Kbuf, Len);

Retn Len;

}

Do you see the problem?

4. Extensions, truncation, and overflow

When you convert an unsigned number to a larger data type, simply add 0 at the beginning, which is called a 0 extension. Converting a signed number to a larger data type requires a symbol extension, which expands the sign bit to the desired number of digits. The result of expanding the 4-bit binary number 1001 (-7) to 8 bits is 11111001 (-7).

When converting a large data type to a small data type, both the unsigned number and the signed number are simply bit truncation. The number of unsigned numbers may change due to truncation, while the number of symbols may vary, and the sign bit may change, such as the 8-bit binary number 00011001 (25) conversion to 4-digit truncation results in 1001 (-7).

When arithmetic operations of integers are performed, when the number of bits of the resulting variable is insufficient to hold the actual actual result, the result of the operation is overrun by truncation, if the 4-bit binary number operation 1011 (-5) + 1011 (-5) = 10110 (-10), However, if the result is also used in 4-bit binary storage, it will be truncated to 0110 (6), resulting in overflow.

When the data type is converted, it is necessary to convert between different data sizes, as well as between unsigned and signed, the C language standard requires the conversion of data size before unsigned and signed conversions.

Signed number and unsigned number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.