Signed number and unsigned number------C + + programming principle and practice (Advanced article)

Source: Internet
Author: User

Program design principles for signed and unsigned numbers:

    • Use a signed number (such as int) when you want to represent a numeric value.
    • When you need to represent a bit collection, use an unsigned number (such as unsigned int).

mixed operations with signed and unsigned numbers can have disastrous consequences . For example:

vector<int> v;for (int i=0;i<v.size (); ++i) cout<<v[i]<< ' \ n ';

Easy to implement version:

unsigned char max=160;    Very large for (signed Char i=0;i<max;i++) cout<<int (i) << ' \ n ';

The loop variable I may overflow, that is, v.size () is more likely to be larger than the largest number of signed int values. When the value of I increases to the maximum positive number that a signed int can represent (for example, an int is 16-bit width, this value is 215-1), the next increment of 1 does not get a larger integer value, and a negative is obtained. So the cycle will never stop! Every time we reach the maximum integer, we start again from the smallest negative int value. So if the value of V.size () is 32*1024 or larger and the loop variable is a 16-bit int, the loop is a (possibly very serious) bug. If the loop variable is a 32-bit int, the same problem occurs when the value of v.size () is greater than or equal to 2*1024*1024*1024.

To avoid this problem, we can use the Size_tupe or iterator provided by the vector:

For (Vector<int>::size_type i=0;i<v.size (); ++i) cout<<v[i]<< ' \ n '; for (VECTOR<INT>:: Iterator P=v.begin ();p!=v.end (); ++p) cout<<*p<< ' \ n '; for (int x:v) cout<<x<< ' \ n ';

Size_tupe is guaranteed to be unsigned, so the first form (using unsigned numbers) is more than the version of the INT-type loop variable, with a bits that represents the value of the loop variable (not the symbol). This improvement is important, but after all it is just one more to indicate the scope of the loop (one more times the number of cycles). This limitation does not exist for versions that use iterators.

Roughly speaking, we have two reasons to use unsigned numbers as integers instead of simply as a set of bits (i.e., not using + 、-、 * and/);

    • There are more bits to represent values, resulting in higher accuracy.
    • Used to represent a logical attribute whose value cannot be negative.

The former is what we have just seen, using unsigned loop variables to bring the effect.

The problem with mixing signed and unsigned numbers is that in C + +, the way they are converted is strange and difficult to remember. For example:

unsigned int ui=-1;int si=ui;int si2=ui+2;unsigned ui2=ui+2;

Output Result:

4294967295-111

It is not recommended to obtain an additional bits precision but to have unsigned numbers.

The subscript of the standard reservoir is an unsigned number.

Principles and Practice of C + + programming (Advanced article)

Signed number and unsigned number------C + + programming principle and practice (Advanced 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.