How to add an unsigned integer all data position to 1

Source: Internet
Author: User

Positive solution "x = -1;"

-1 exceeds the expression range of the unsigned integer x, according to the standard (c++11 section 4.7.2), 1 will be converted to 2^n-1 (n is the number of bits of x).

Non-positive solution "x = 0xffffffff;"

This applies only to the 32bit integer type.

Non-positive solution "x = ~0;"

This is a lot of books as a "standard answer" answer is also wrong. Three negative representations of the original code/inverse code/complement are supported in the C + + standard, and the correct result is obtained only in the complement environment. (for example, under anti-code, ~0 will get -0,-0 to unsigned still 0)

So x = ~0u ?

is still wrong. According to the standard (c++11 section 2.14.2.2), the literal 0u is unsigned int. Then the inverse is Uint_max, if x can represent the maximum value is not Uint_max then the result is wrong.

How to add an unsigned integer all data position to 1

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.