In-depth understanding of computer systems (2.5)------The number of signed and unsigned numbers in the C language, as well as the expansion and truncation of numerals

Source: Internet
Author: User

Previous blog We have explained the representation of integers in computers, including unsigned and complementary codes, and the conversion between them, which is a very important point of knowledge for individuals. In this blog we will introduce the signed and unsigned numbers in C and the expansion and truncation of numbers.

1. Signed number and unsigned number in C language

Previous blog We have given the C language in the 32-bit machine and 64-bit machine support of the integer type of data, we here only give the 32-bit machine:

  

Although the C language standard does not specify a symbolic number to be represented by some encoding, almost all machines use a complement. Most numbers are usually signed by default, such as when declaring a constant like 12345 or 0xABC, this value is considered to be signed.

The C language allows conversions between signed and unsigned numbers. On a complement-based machine:

  ①, unsigned number converted to signed number

    

  ②, signed number converted to unsigned number

    

We can look at the following program:

1234567891011121314 #include <stdio.h> int main(){    char t = 0xFF;    //%d把对应的整数按有符号十进制输出,%u把对应的整数按无符号十进制输出    //有符号的转换成无符号的     printf("t=%d,t2u=%u\n",t,(unsigned char)t);     unsigned char u = 0xFF;    //%d无符号转换成有符号的     printf("u=%u,u2t=%d\n",u,(char)u);    return 0;}

The result is:

  

Why this result, I was in a blog: in-depth understanding of the computer system (2.4)------integer representation (unsigned encoding and complement encoding) has been said, this is the data type of the cast .

The second case is that when one type of expression is assigned to a variable of another type, the conversion is implicit. Like what:

12345678910 #include <stdio.h>int main(){    unsigned char u = 0xFF;    char t = u;    //%d无符号转换成有符号的是默认的     printf("u=%u,u2t=%d\n",u,t);    return 0;}

The result is:

  

We assign an unsigned number to a signed, and its conversion is implicitly occurring. This is not a difference for standard operations, but for relational operations like < and >, it can lead to incorrect results.

  Note: In the C language, when an operation is performed, the signed arguments are implicitly strongly converted to unsigned parameters. 

12345678 #include <stdio.h>  int main () {       printf ( ,- 1 <0u); //The result is 0,0 indicates that error 1 is correct &NBSP;&NBSP;&NBSP;&NBSP; printf ( "%d\n" ,- 123 <123u); //0 &NBSP;&NBSP;&NBSP;&NBSP; return 0 }

We explain why the first -1 < 0u is wrong. Since 0u is unsigned, 1 is signed. Then 1 will be converted to unsigned.

namely T2uw ( -1) =-1+232=4 294 967 296-1=4 294 967 295

Then -the 1 < 0u expression also becomes 4 294 967 295u < 0u , the result is of course wrong. We can also analyze the second example, which is not described in detail here.

Therefore, we should pay attention to the actual coding process due to the implicit conversion caused by the error operation.

2, expand the bit representation of a number

To extend a bit of a number, simply to convert between integers of different lengths, which we may need to keep the values unchanged. Of course, when converting a data to a data type with a smaller word length, its value will definitely change. Then we can only convert the smaller data types to the larger data types. For example, a short int is converted to an int of integer type. , what should we do?

①, 0 extensions

To convert an unsigned number to a larger data type, we simply add 0 to the front of the binary sequence.

②, sign bit extension

To convert a complement number to a larger data type, we need to add the sign bit at the beginning.

We can conclude from the above two articles: if our primitive bits are [xw-1, xw-2, ..., x2, x1, x0], then the extension can be expressed as: [Xw-1, Xw-1,..., xw-1, xw-2, ..., x2, x1, x0].

That is, we want to prove that:

  

On the left side of the expression, we added a xw-1 copy of K-bit. If we prove that the sign bit expands one bit, that is k=1, the value remains the same. It is possible to maintain this attribute for any k. Then the equation becomes:

  

Since unsigned, add 0, this is well understood, and the values are unchanged. Then we prove the symbolic complement code:

Because:

Replace the above complement code with the right side of the equation, i.e.:

The above proof we just need to know:2w-2w-1=2w-1 is very well understood.

3. Truncation of Numbers

This is just the opposite of the extension above. That is, we do not need to expand the bits of a number, but to reduce the number of bits of a number.

  When you truncate a W-bit number [xw-1, Xw-2, ..., x2, x1, x0] to a K-bit number, we discard the high w-k bit. Get [Xk-1, Xk-2, ..., x2, x1, x0]

For unsigned truncation, the formula is:

  

The certification process is as follows:

    

For the truncation of the signed (complement code), we just need to add a step to convert the unsigned code to the complement code.

    

For example, the following program:

12345678910 #include <stdio.h>int main(){    int i = 53191;    short int j = (short)i;     int k = j;    printf("%d %d %d\n",i,j,k);    return 0;}

The result is:

  

We convert the I strong to short int, on a 64-bit machine, the 32-bit int is truncated to a 16-bit short int, and this 16-bit pattern is the complement representation of 12345. When we turn it into an int, the sign bit extension sets the high 16 bits to 1, resulting in a 12345 32-bit complement representation.

4. Summary

This blog explains the number of signed and unsigned numbers in C and how to extend and truncate a numeric value, and it is necessary to understand their principles.

We have seen from above the peculiarities of many unsigned operations, especially the implicit conversion of signed numbers to unsigned numbers will result in errors. The way to avoid such errors is to not use unsigned numbers. In fact, in addition to the C language, few languages support unsigned numbers. For example, Java only supports integer data and requires a complement operation.

So the number of integers in the computer is finished, the next blog will explain the arithmetic of the integer in the computer, the two numbers we appear will produce inexplicable results in the next blog will be answered.

In-depth understanding of computer systems (2.5)------The number of signed and unsigned numbers in the C language, as well as the expansion and truncation of numerals

Related Article

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.