"C + +" type elevation __c++

Source: Internet
Author: User
#include <iostream>
#include <string.h>

using namespace std;

int main ()
{
char a =-1;
unsigned char b = 255;
printf ("a = 0x%x b = 0x%x \ n", A, b);
System ("pause");
return 0;
}


The results of the operation are as follows:



Analytical:

The reason that the two are not the same as the output is because%x is a 16-in-print unsigned int (unsigned int). Therefore, when printing, there are two implicit type conversions:

Note 1:

Because in the case of coding as complement, there are two types of elevation: 1. Symbolic extension: For a signed number, the method of extending the storage bit number. Fills the new High-order byte with the value of the current most significant bit, the sign bit. (sign bits are all in 0 for "positive" and 1 for "negative" 2.) 0 extension: For unsigned numbers, the method of extending the storage bit number. Fill in the new high 0 directly. NOTE 2:-5 corresponds to positive 5 (original code: 00000101) → all bit counter (reverse code: 11111010) → plus 1 (complement: 11111011)

1, Char => unsigned int:a is stored in the computer system as 0xFF, because it is a symbolic extension, so the number of unsigned integers is: 0xFFFFFFFF.

2, unsigned char => unsigned int:b stored in the computer system as 0xFF, because it is 0 expansion, so the number of unsigned integers is still: 0xFF.


Reference blog:

Type promotion in C--basic concept, but there's still a lot of people who can't figure out

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.