C in signed and unsigned

Source: Internet
Author: User

1 int i=3; 2 cout<<i *-1;
ask what the result is.

First reaction:-3. But the result does not seem like this, wrote a program, ran a bit, found that:4294967293.

1) on a 32-bit machine, the int and unsigned int are 32 bits (4 bytes). 2) enum will be determined by the maximum value of the type, generally int, if the range that can be represented by the int type, it is represented by the smallest type larger than the int type (unsigned int, long or unsigned long)3) about the size of the type. The size of the type is generally compared with the range of data that can be represented, such as char type <unsigned char type <short type ... In expressions, it is generally converted from a small type to a large type (except coercion type conversions) . The following combination of their own information, coupled with their own constantly to give a variety of programming, summed up about the type conversion (only in arithmetic expressions about the integer type of conversion) some problems (if there is missing, welcome to add, grateful)1. All data types that are smaller than the int type (including char,signed char,unsigned char,short,signed short,unsigned short) are converted to int type. If the converted data exceeds the range represented by the int type, it is converted to the unsigned int type;2, bool conversion to int, false conversion to 0,true conversion to 1, in turn all the integer type converted to bool, 0 to False, and other non-0 values to true;3, if the expression is mixed with unsigned short and int type, if the int data can represent all unsigned short type, then the unsigned short type of data will be converted to int type, otherwise, unsigned Both the short type and the int type are converted to unsigned int types. For example, on a 32-bit machine, int is 32 bits, the range –2,147,483,648 to 2,147,483,647,unsigned short is 16 bits, the range is 0 to 65,535, so the int is sufficient to represent unsigned Short type of data, so in the mixed with the two operations, unsigned short type data is converted to int;4, unsigned int and long type of conversion law with 3, on the 32-bit machine, the unsigned int is 32 bits, the range 0 to 4,294,967,295,long is 32 bits, the range –2,147,483,648 to 2,147,483,647, the long type is not enough to represent all unsigned int, so in an expression mixed with unsigned int and long, both are converted to unsigned long;5. If both int and unsigned int are present in an expression, all int data is converted to the unsigned int type.  after this summary, the answers to the questions raised above should be obvious. In the expression i*-1, I is the unsigned int, 1 is an int (the type of the constant integer is the same as the enum), and by 5th it is known that 1 must be converted to the unsigned int, or 0xFFFFFFFF, the decimal 4294967295, and then multiply with I, namely 4294967295*3, if overflow is not considered, the result is 12884901885, hexadecimal 0x2fffffffd, because unsigned int can only represent 32 bits, So the result is 0xfffffffd, which is 4294967293. in the C language, signed requires that the highest bit is the sign bit, the following represents the data size, and the unsigned all bits represent the size. If you use 8-bit binary notation, the signed range is 128 to 127,unsigned.

The 0 to 255,c language specifically uses two keywords to describe two kinds of representations, so there are some incredible questions.

1. Overflow

In the symbolic operation may produce overflow problem, summed up is: two integers can overflow, the addition of two negative numbers may overflow, a positive and negative addition will certainly not overflow. See an interesting question in the C depth analysis.

1#include <stdio.h>2#include <string.h>3 intMainvoid)4 {5     Chara[ +];6     intK =0;7      for(; k < +; k++) 8     {9A[i] =-1-i; Ten  One } Aprintf"%d\n", strlen (a)); -     return 0; -}

The final result is 255. Because the array a[1000] is of type char, it is explicitly specified in the C language that the char type takes up a byte of memory space, and on x86 's GCC platform char defaults to signed, beginning with K=0,a[0]=-1, with K increasing, when k=127, a[127]= -128, the corresponding binary is 10000000, we know-128 is the compiler can represent the minimum value, when k=128,a[128] of course it is not possible to store-129 this value, because the highest bit overflow, so in the computer store the complement value is 01111111,. As K continues to increase, when k=254, a[254] in the computer to store the complement is 00000001, and k=255,a[255] corresponding storage value is 00000000, that is 0,strlen function encountered the first 0 stop , All the final results are k from 0 to 254, with a total length of 255.

2, signed and unsigned mixed operations

In addition to the char type in the C language, the compiler defaults to other integral types, which are signed, and all integral types in the x86 GCC platform include Char are signed.

1#include <stdio.h>2 3 intMainvoid)4 {5unsigned a =Ten;6Unsigned B =-Ten;7 if(a) printf ("yes\n");Elseprintf"no\n");8 if(b) printf ("yes\n");Elseprintf"no\n");9 Ten intc =b; Oneprintf"%d\n", c); A if(c) printf ("yes\n");Elseprintf"no\n"); -  - intD =- -; the intE = a +D; -printf"%d\n", e); - if(e) printf ("yes\n");Elseprintf"no\n"); -  + return 0; -}

The final result is

Yes
Yes
-10
Yes
-10
Yes

As can be seen from the above example, in C, the signed number can be assigned to the unsigned number, the result is an unsigned number, and the unsigned number can also be assigned to the signed number, the result is an unsigned number; In a mixed operation, as long as there is an unsigned number, the signed number will be converted into an unsigned number to participate in the operation The results are also saved as unsigned.

Note: signed and unsigned are stored in the same way in the computer, except that the method is interpreted differently, that is, a signed and unsigned.

%d prints the signed type, and%x is the UNSIGND type of the print, with a different print equivalent to a type conversion. Of course, C + + is more intuitive and can automatically identify types and print values directly.

Just use the example above:

3 ;p rintf ("%u\n", (c* (-1)));       // The   print out is -3    printf ("%u\n", (c* (-1)));       //   The print out is 4294967293 

C in signed and unsigned

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.