A written question by Dahua Corporation C/C ++

Source: Internet
Author: User

After the program is debugged, the result is 5,-7,-2;

#include"stdio.h"#include"iostream"using namespacestd;int main(void){      unsigned int a=5;        int b=-7,c=4;        if(b+a>0)               c=a+b,b=c-a;        else               c=a-b,b=c+a;        cout<<a<<","<<b<<", "<<c<<endl;                return 0;}

The result is c = 12, B = 17, and a = 5. This is mainly because B + A equals-2 is less than zero and is executed in else. After one-step debugging in Visual C ++, it is found that the program enters the if statement after the if statement is determined. That is, B + A is greater than zero. Why didn't I enter else? After looking at the code again, we know that the unsignedint type is used for variable A, while B is of the int type. What type is the result after the sum of the two? This involves a problem of upgrading the type when calculating different types of variables!

When two variables of different types are added, the result type should follow the principle below: In the added variables, the result is converted to which type of variable can represent a larger value. SHORT + long, the result is converted to long. The sum of unsigned int and INT types. on 32-bit machines, the maximum value of unsigned int can be 2 ^ 32-1, and the maximum value of int can be 2 ^ 31-1. because the result should be converted to the unsignedint type. In this way, int is upgraded to unsignedint.

Unsigned int A = 5; int B =-7; B is a negative value, and the highest bit is 1. After being converted to an unsigned integer, the highest bit does not represent a symbol, but a value of this type, is a very large positive number, and the result of adding both is also a large integer. So it is natural to enter the if statement.

In the IF and else branches, a comma expression is used, for example:

C = A + B, B = C-A; execute c = a + B first, and then execute B = C-a. the final value of the entire expression is the value of the last expression, if int resullt = (C = A + B, B = C-A), the result is the same as the value of B.

Binary representation:

Unsignedint A = 5; red

Int B =-7; black

Makeup: 00000000 00000000 00000000 00000101

Original code: 10000000 00000000 0000000 00000111

Makeup: 111111111 11111111 11111111 11111001

Complement: 11111111 11111111 11111111 11111110

Converts data to C, where C is an int;

The highest bit is 1, which is a negative number. The complement code is converted to the original code.

First subtract multiple reverse attempts

11111111 11111111 11111111 111111101

10000000 00000000 00000000 00000010

-2 is the result of C.

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.