Computing traps between unsigned and signed numbers

Source: Internet
Author: User

Cout <(1>-2) <Endl; // 1 normal, all are signed numbers
Cout <(unsigned INT) 1>-2) <Endl; // 0-2 is converted to an unsigned number.
Cout <(unsigned INT) 1>-2.) <Endl; // 1 float does not have an unsigned number. Therefore, the unsigned number must be greater than the negative number of the float type!

///////////////////////////////
Http://www.52rd.com/Blog/Detail_RD.Blog_lentil_13982.html
The following experiments run in virual C ++ 6
Test whether the C LanguageAutomatic Integer Conversion PrincipleSome developers know little about these things. All operands are automatically convertedUnsigned type. Therefore, in this sense, the computing priority of the unsigned number is higher than that of the signed number, which is very important for embedded systems that should frequently use the unsigned data type.
First, conduct an experiment to define a signed int type data and an unsigned int type data respectively, and then compare the size:
Unsigned int A = 20;
Signed int B =-130;
A> B? Or B>? The experiment proves that B> A, that is,-130> 20, why is this result?
This is because in C language operations, if there is an operation between the unsigned number and the signed number, the compiler will automatically convert it to the unsigned number for processing. Therefore, a = 20, B = 4294967166. In this case, B>.
Another example:
Unsigned int A = 20;
Signed int B =-130;
STD: cout <a + B <STD: Endl;
The result output is 4294967186. Similarly, before calculation, a = 20 and B are converted to 4294967166, so a + B = 4294967186

Subtraction is similar to multiplication.

If B =-130 is used as the signed int data, the operation between B and the immediate number does not affect the type of B, and the operation result is still signed INT:
Signed int B =-130;
STD: cout <B + 30 <STD: Endl;
The output is-100.

For floating point numbers, float (double) is actually a signed number. The unsigned and signed prefixes cannot be added to float and double, of course, there is no conversion problem between the unsigned numbers.

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.