Comparison between signed numbers and unsigned 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!

///////////////////////////////
Html> http://www.52rd.com/Blog/Detail_RD.Blog_lentil_13982.html
The following experiments run in virual c ++ 6
To test whether the principle of automatic Integer Conversion in the C language is understood, some developers know very little about it. When the expression contains a signed or unsigned type, all operands are automatically converted to the unsigned 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.

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.