Some insights of C + +/C numeric types

Source: Internet
Author: User

Contrary to the other answers, there are no undefined behavior here, and there are no overflow. Unsigned integers use modulo 2n arithmetic.

Section 4.7 Paragraph 2 of the standard says "If the destination type was unsigned, the resulting value is the least unsign Ed integer congruent to the source integer (modulo 2n where n was the number of bits used to represent th e unsigned type). " This dictates that-1 are equal to the largest possible unsigned int (modulo 2n).

Section 3.9.1 paragraph 4 says "Unsigned integers, declared Unsigned, shall obey the laws of arithmetic modulo 2 n  where  n  is The number of bits in the value representation of this particular size of integer. " To make it clear about this means, the footnote to this clause says "This implies that unsigned arithmetic does not OVERFL ow because a result that cannot being represented by the resulting unsigned integer type was reduced modulo the number is One greater than the largest value that can is represented by the resulting unsigned integer type. "

In other words, converting-1 to 4294967295 are not just defined behavior, it's required behavior (assuming t integers). Similarly, adding 3 to that value and yielding 2 as a result is also required behavior. The value of n is irrelevant. The third value printed by must are 2 or the implementation is not compliant with the standard hello() .

Some insights of C + +/C numeric types

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: 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.