In C, signed and unsigned, signedunsigned

Source: Internet
Author: User

In C, signed and unsigned, signedunsigned

1 unsigned int i=3;2 cout<<i * -1;
What is the result.

First Response:-3. But the result does not seem to be like this. I wrote a program and ran it. I found it was 4294967293.

1) on a 32-bit machine, the int and unsigned int types are both 32-bit (4 bytes ). 2) enum will determine the type based on the maximum value. Generally, it is int type. If it is beyond the range expressed by int type, it is represented by the smallest type greater than int type (unsigned int, long or unsigned long) 3) about the type size. Generally, the data range that can be expressed is used to compare the size of the type, such as char <unsigned char <short type... in expressions, it is generally converted from a small type to a large type (except for forced type conversion). In combination with the information you have checked, add your own programming in various situations, I would like to sum up some questions about type conversion (only the integer type conversion in arithmetic expressions). (If you have any questions, please add them. Thank you) 1. All data types smaller than int type (including char, signed char, unsigned char, short, signed short, and unsigned short) are converted to int type. If the converted data exceeds the range expressed by the int type, it is converted to the unsigned int type. 2. When the bool type is converted to the int type, false is converted to 0, true is converted to 1. In turn, when all integer types are converted to bool, 0 is converted to false, and other non-zero values are converted to true. 3. If the expression contains unsigned short and int types, if int type data can represent all unsigned short types, the unsigned short type data is converted to int type. Otherwise, the unsigned short type and int type are converted to unsigned int type. For example, on a 32-bit machine, the int value ranges from-2,147,483,648 to 2,147,483,647, And the unsigned short value ranges from 0 to 65,535, in this way, the int type is sufficient to indicate the unsigned short type data. Therefore, in the mixed operation of the two types, the unsigned short type data is converted to the int type; 4. The conversion rules of the unsigned int and long types are the same as 3. On a 32-bit machine, the unsigned int Is 32 bits, the range is 0 to 4,294,967,295, and the long is 32 bits, and the range is-2,147,483,648 to 2,147,483,647, it can be seen that the long type is not enough to indicate all unsigned int types. Therefore, in the expressions mixed with unsigned int and long, both are converted to unsigned long; 5. If the expression contains both int and unsigned int, all int data is converted to unsi. Gned int type. After this conclusion, the answer to the question raised above should be obvious. In expression I *-1, I is of the unsigned int type and-1 is of the int type (the constant Integer type is the same as enum ), according to the 5th records, we can know that-1 must be converted to the unsigned int type, that is, 0 xffffffff, decimal 4294967295, and then multiplied by I, that is, 4294967295*3. If overflow is not considered, the result is 12884901885, with the hexadecimal 0x2fffffd. Since the unsigned int can only represent 32 bits, the result is 0 xfffffffd, that is, 4294967293. In C, signed requires that the highest bit is the symbol bit. The following indicates the data size, while unsigned indicates the size of all digits.If an 8-bit binary representation is used, the signed range is-128 to 127, and the unsigned is

From 0 to 255, the C language uses two keywords to describe the two representation methods, which leads to some incredible problems.

1. Overflow

In a symbolic operation, overflow may occur. In conclusion, the addition of two integers may overflow, and the addition of two negative numbers may also overflow. The addition of one positive and one negative will certainly not overflow. I saw an interesting question in in-depth analysis of C.

 1 #include <stdio.h> 2 #include <string.h> 3 int main(void) 4 { 5   char a[1000]; 6   int k = 0; 7   for (; k < 1000; k++)  8     { 9       a[i] = -1-i;  10 11   }12   printf("%d\n", strlen(a));13   return 0;14 }

The result is 255. Because array a [1000] is of the char type, the C language explicitly specifies that the char type occupies a byte memory space, and char is signed by default on the x86 gcc platform, k = 0, a [0] =-1, as k increases, when k = 127, a [127] =-128, the corresponding binary value is 10000000, we know that-128 is the minimum value that the compiler can represent. When k = 128, a [128] certainly cannot store-129, because the maximum bit overflows, therefore, the supplemental value stored in the computer is 01111111 ,. As k continues to increase, when k = 254, a [254] is stored in the computer with a completion code of 00000001, while k = 255, a [255] corresponds to a storage value of 00000000, that is, 0,The strlen function stops when it encounters the first 0., All the final results are k from 0 to 254, and the total length is 255.

2. Mixed signed and unsigned operations

In C language, except for the char type, all the integer types in the compiler are signed by default. All Integer types, including char, are signed on the x86 gcc platform.

 1 #include <stdio.h> 2  3 int main(void) 4 { 5 unsigned a = 10; 6 unsigned b = -10; 7 if (a) printf("yes\n"); else printf("no\n"); 8 if (b) printf("yes\n"); else printf("no\n"); 9 10 int c = b;11 printf("%d\n", c);12 if (c) printf("yes\n"); else printf("no\n");13 14 int d = -20;15 int e = a + d;16 printf("%d\n", e);17 if (e) printf("yes\n"); else printf("no\n");18 19 return 0;20 }

The final result is

Yes
Yes
-10
Yes
-10
Yes

From the example above, we can see that in the C language, the number of symbols can be assigned to the number of unsigned numbers, and the result is an unsigned number, and the number of unsigned numbers can also be assigned to the number of symbols, the result is still an unsigned number. In a hybrid operation, any unsigned number is converted into an unsigned number, and the result is saved as an unsigned number.

 

Note:The storage formats of signed and unsigned are the same on the computer, but the interpretation methods are different, that is, a signed and unsigned.

% D prints the signed type, while % u prints the unsignd type. Different prints are equivalent to a type conversion. Of course, C ++ is intuitive and can automatically identify the type and print the value.

The following example is used:

Unsigned c = 3; printf ("% d \ n", (c * (-1 ))); // print-3 printf ("% u \ n", (c * (-1); // print 4294967293

 

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.