[C/C ++]

Source: Internet
Author: User

[C/C ++]

Positive Solution:

X =-1;

-1 is out of the expression range of the unsigned integer x. According to the standard (C ++ 11 section 4.7.2),-1 is converted to 2 ^ N-1 (n is the number of bits of x ).

 

Solution:

X = 0 xFFFFFFFF;

This is only applicable to 32bit integers.

 

Solution:

X = ~ 0;

This is also the answer that many books recognize as a "standard answer. The C/C ++ standard supports three negative representation forms: original code, reverse code, and complement code. The correct result is obtained only in the complement environment. (For example, in the back code ,~ 0 will get-0,-0 to unsigned, still 0)

Then x = ~ What about 0u?

Still wrong. According to the standard (C ++ 11 section 2.14.2.2), the literal 0u is an unsigned int. UINT_MAX is obtained after the inversion. If the maximum value that x can represent is not UINT_MAX, the result is wrong.

 


In C, if an integer occupies four bytes in memory, which value range of the unsigned integer data is used?

2 ^ 31-1

In C, you can assign a negative value to an unsigned integer variable.

Yes, but the result will go wrong. The maximum digit of the negative number in the computer is 1, which is used as a symbol sign (positive number is 0). However, if you assign it to an unsigned integer variable, the flag of the highest bit is a numerical bit, which is regarded as a numerical value during calculation.

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.