Forcing type conversion issues

Source: Internet
Author: User
Tags modulus rounds

In C + +, when a value of a type that represents a larger range is assigned to a type that represents a smaller range, the type conversion is automatically enforced and converted to a range that represents a larger type.

The law of conversion is that the number of values represented by a smaller type is assumed to be N. The converted value is the remainder after a modulo n.

For the integer number, a, the method of modulo operation or redundancy operation is:

1. Seek integer quotient: c = A/b;

2. Calculate the modulus or remainder: R = a-c*b.  The modulo operation and the remainder operation are different in the first step: when the take-rest operation takes the value of C, it rounds to 0 (fix ()), and the modulo operation rounds the negative Infinity direction when calculating the value of C (floor () function). For example: Calculate -10 Mod 4 Then: a = -10;b = 4; The first step: to find the integer quotient C, such as the modulo operation C = 3 (rounded to the negative infinity direction), the remainder c = 2 (rounded to 0 direction); The second step: Calculate the formula and remainder of the same equation, but because the value of C is different, the modulus of R =  When r =-2.  Induction: When the A and B symbols are consistent, the values of the C value of the modulo operation and the remainder operation are consistent, so the results are consistent. When the symbols are inconsistent, the results are different.  The symbol of the result of the modulo operation is consistent with B, and the sign of the result of the remainder operation is consistent with a. In addition, the meaning of the% operator in each environment is different, for example, C/c++,java is the remainder, while Python is the modulo.
1#include <iostream>2 using namespacestd;3 intMain ()4 {5unsigned u=Ten;6     intI=-11;7cout<<u+i<<Endl;8     return 0;9}

2^32=4294967296,i to 4294967296 to take the surplus to 4294967296-11=4294967285

So the result is 4294967285+10=4294967295.

1#include <iostream>2 using namespacestd;3 intMain ()4 {5unsigned u=Ten;6     inti=-9;7cout<<u+i<<Endl;8     return 0;9}

As the code above, the difference is that the value of int is changed to-9 (it looks like the absolute value is smaller than u)

I take the 4294967296-9=4294967287 of the remainder

So the result is 4294967287+10=4294967297, exceeding the 2^32, so the result is 4294967297 to 2^32, the result is 1.

When an unsigned number is subtracted, it is actually performed by a signed operation, and then the type is converted

1#include <iostream>2 using namespacestd;3 intMain ()4 {5unsigned u1= A, u2=Ten;6cout<<u1-u2<<Endl;7cout<<u2-u1<<Endl;8     return 0;9}

The first output is undoubtedly 2.

The second output is signed as-2, and the value after conversion to unsigned is 4294967296-2=4294967294.

Forcing type conversion issues

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.